Table of Contents

Inheritance Example

This page contains an example of a family of classes that make use of inheritance, interfaces, abstract classes, and polymorphism. On this page we will:

Meet the Example

This example consists of the following:

UML Class Diagram

We'll describe the specific aspects of the UML diagram in more detail below, but here is what it looks like:

Abstract DesktopItem Class

public abstract class DesktopItem {
 
	private String itemName;
 
	private DesktopItem(){
 
	}
 
	public DesktopItem(String name){
		itemName = name;
	}
 
	public abstract void erase();
 
	public String getItemName(){
		return itemName;
	}
 
	public abstract void open();
 
	public void rename(String name){
		itemName = name;
	}
 
	public int size(){
		return itemName.length();
	}
 
}

Simple TextFile Subclass

public class TextFile extends DesktopItem {
 
	private String text;
 
	public TextFile(String name){
		super(name);
	}
 
	private TextFile(){
		super("broken");
	}
 
	public void erase(){
		text = null;
	}
 
	public void open(){
		System.out.println(getItemName() + ": " + text + " [TextFile.open()]");
	}
 
	public int size(){
		return super.size() + text.length();
	}
 
}

Folder Subclass

import java.util.LinkedList;
import java.util.List;
 
public class Folder extends DesktopItem implements Group {
 
	private List<DesktopItem> items;
 
	public Folder(String name){
		super(name);
		items = new LinkedList<DesktopItem>();
	}
 
	private Folder(){
		super("broken");
	}
 
	public void add(DesktopItem item){
		items.add(item);
	}
 
	public void erase(){
		items.clear();
	}
 
	public void open(){
		System.out.println("Beginning Folder.open() on: " + getItemName());
		for(DesktopItem item : items)
		{
			item.open();
		}
		System.out.println("Ending Folder.open() on: " + getItemName());
	}
 
	public int size(){
		int size = super.size();
		for(DesktopItem item : items)
		{
			size += item.size();
		}
		return size;
	}
 
}

Group Interface

public interface Group {
 
	public void add(DesktopItem item);
 
}

Client Application

public class MyApp {
 
	public static void main(String[] args) {
 
		Folder root = new Folder("My Documents");
		Folder se1020 = new Folder("SE1020");
		Folder ce1900 = new Folder("CE1900");
 
		DesktopItem item = new TextFile("Lab1.xml");
		se1020.add(item);
		item = new TextFile("Lab1.xml");
		item.rename("Lab2.xml");
		se1020.add(item);
		se1020.add(new TextFile("Lab3.xml"));
		se1020.add(new TextFile("Lab4.xml"));
		se1020.add(new TextFile("Lab5.xml"));
		se1020.add(new TextFile("Lab6.xml"));
		se1020.add(new TextFile("Lab7.xml"));
 
		ce1900.add(new TextFile("Lab1.txt"));
		ce1900.add(new TextFile("Lab2.txt"));
		ce1900.add(new TextFile("Lab3.txt"));
		ce1900.open();
 
		root.add(se1020);
		root.add(ce1900);
 
		root.open();
 
	}
 
}

Output from Client Application

Beginning Folder.open() on: CE1900
Lab1.txt: null [TextFile.open()]
Lab2.txt: null [TextFile.open()]
Lab3.txt: null [TextFile.open()]
Ending Folder.open() on: CE1900
Beginning Folder.open() on: My Documents
Beginning Folder.open() on: SE1020
Lab1.xml: null [TextFile.open()]
Lab2.xml: null [TextFile.open()]
Lab3.xml: null [TextFile.open()]
Lab4.xml: null [TextFile.open()]
Lab5.xml: null [TextFile.open()]
Lab6.xml: null [TextFile.open()]
Lab7.xml: null [TextFile.open()]
Ending Folder.open() on: SE1020
Beginning Folder.open() on: CE1900
Lab1.txt: null [TextFile.open()]
Lab2.txt: null [TextFile.open()]
Lab3.txt: null [TextFile.open()]
Ending Folder.open() on: CE1900
Ending Folder.open() on: My Documents

UML Class Diagrams