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:

  • Review inheritance, interfaces, abstract classes, and polymorphism.
  • Introduce additional UML notation.

Meet the Example

This example consists of the following:

  • A class, MyApp, that contains a main method.
  • An abstract class, DesktopItem, which describes an item that sits on your computer's desktop (e.g., hard drive, text file, folder, MP3 file, trash can, etc…). Since the class is abstract, subclasses must provide the additional implementation for specific kinds of desktop items.
  • Two subclasses of the DesktopItem class:
    • TextFile which represents a file containing text.
    • Folder which represents a directory.
  • An interface, Group, that contains one method: add(DesktopItem)

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

  • The name of an abstract class appears in italics. See DesktopItem.
  • The name of an interface appears in italics and is preceded by «interface». See Group.
  • The name of an abstract method appears in italics. See DesktopItem.erase().
  • The name of a class (static) attribute is underlined. See MyApp.main(String[]).
  • The inheritance relationship is shown using a line with an open arrow pointing from the subclass to the superclass.
  • A class implementing an interface is shown using a dotted line with an open arrow pointing from the class to the interface it implements.
  • A class may indicate that it makes use of another class/interface using a dotted line with an arrow pointing to the class that is used.
  • A class indicates that it is contains object(s) from another class as a field using a line: (See line from Folder to DesktopItem)
    • At the end of the line next to the class that contains the field should be a solid diamond.
    • At the end of the line next to the class that is contained, a number, or range of numbers, indicates how many of the objects are contained. In this example, the Folder can contain zero or more DesktopItems.
se1020/inheritanceexample.txt · Last modified: 2009/06/03 11:22 (external edit)
 

This website is not owned or managed by the Milwaukee School of Engineering.

© 2003-2010 Dr. Christopher C. Taylor, et. al. • Office: L-343 • Phone: 277-7339 • npǝ˙ǝosɯ@ɹolʎɐʇ • -> RSS <-