Chapter 9
|
Chapter 8 introduced inheritance. Chapter 9 discusses key elements of inheritance - method overriding and polymorphism.
| Polymorphic - Many shapes. |
Polymorphic method- When inherited methods are over-ridden, the type the object references determines the method executed.
| Variables and subtypes - Variables reference objects of their type or subtype. Cannot reference (i.e. be assigned) supertype objects. |
| Substitution - Subtype objects can be substituted wherever supertype object allowed. |
overriding - A subclass method of the same name as a super class method will be called for the subclass object. The sub class method has precedence over the super class method.
Method Polymorphism - When a method is overridden (i.e. methods with the same name in an inheritance hierarchy), the object's class determines which of the methods is called.
Invokes the printTicket() method at line 37.
Invokes the printTicket() method at line 16.
|
|
Exercise 0a - Method Polymorphism Suppose that we have created TicketMachine and BusTicketMachine by:
BusTicketMachine tm1 = new BusTicketMachine( 50, "Number 3", "4th St. - Mall - Airport" );
TicketMachine t2 = new TicketMachine( 60 );
TicketMachine t3 = new BusTicketMachine( 50, "Number 3", "4th St. - Mall - Airport" );
- Are the following valid?
tm1.printStops();
tm2.printStops();
tm3.printStops();
- Trace the execution of:
tm1.printTicket();
tm2.printTicket( );
tm3.printTicket( );
| One | Two | Three |
|
|
|
Exercise 0b - Polymorphism
- What is the hierarchy?
- Trace the execution.
One myOne = new Two("red");
Two myTwo = new Three("white");
Three myThree = new Three("blue");
System.out.print( myOne.getS() );
System.out.print( myTwo.getS() );
System.out.print( myThree.getS() );

You may have noticed that the second DoME example that used inheritance did not print all the data fields of CD and Video. Only the fields of Item class were printed. The reason being that the Item method print() only has access to Item fields.
Database list() method calls:
Item item = (Item) iter.next();
item.print();
| Item | Video | CD |
| public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; public void print() |
public class Video extends Item { private String director; : |
public class CD extends Item { private String artist; private int numberOfTracks; : |
| public class Database { private ArrayList items; public void list() { for(Iterator iter = items.iterator(); iter.hasNext(); ) { Item item = (Item)iter.next(); item.print(); } } } |
Exercise 1 - DoME print problem In BlueJ
- Open chapter08/dome-v2
- Create a Video, CD and Database object.
- Use Database method addItem() to add the CD and Video object to the database.
- Call the Database method list() to print the database.
- Did the Item method print() execute?
- Add print() methods for CD and Video classes.
- Copy and paste print() from below.
- Create a Video, CD and Database object.
- Use Database method addItem() to add the CD and Video object to the database.
- Call the Database method list() to print the database.
- Did the Item method print() execute?
| Item | Video | CD |
| public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; public void print() |
public class Video extends Item { private String director;
: |
public class CD extends Item { private String artist; private int numberOfTracks;
: |
Problem
Solution
We'll see
how shortly.Database list() method calls:
public void list()
{
for(Iterator iter = items.iterator(); iter.hasNext(); )
{
Item item = (Item) iter.next(); // item is CD or Video
item.print(); // Call CD or Video print() method
}
}
static type - The type a variable is declared in the Java source code. Used for type checking at compile time.
dynamic type - The type a variable stores at the current time. Used to determine method to call at runtiime.

overriding - A subclass method of the same name as a super class method will be called for the subclass object. The sub class method has precedence over the super class method. In the following, the print() methods of CD and Video override the print() method of Item for CD and Item objects.
| Item | Video | CD |
| public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; public void print() |
public class Video extends Item { private String director;
: |
public class CD extends Item { private String artist; private int numberOfTracks;
: |

method lookup - How methods are called. v1.print();

v1.print();
v1.print();
In the following, the print() methods of CD and Video override the print() method of Item for CD and Item objects.
To call the print() method of Item for CD and Item objects, the print() methods call super class print() method, Item.
No changes to super class method.
| Item | Video | CD |
| public class Item { private String title; private int playingTime; private boolean gotIt; private String comment; public void print() |
public class Video extends Item { private String director;
: |
public class CD extends Item { private String artist; private int numberOfTracks;
: |
Exercise 2 - Super call in methods In BlueJ
- Open chapter08/dome-v2
- Add super.print() methods to CD and Video method print().
- Create a Video, CD and Database object.
- Use Database method addItem() to add the CD and Video object to the database.
- Call the Database method list() to print the database.
- Did the Item, CD and Video method print() execute?