lecture 19

25
Lecture 19 - Inheritance (Contd)

Upload: nasim-rich

Post on 02-Jan-2016

17 views

Category:

Documents


0 download

DESCRIPTION

Lecture 19. - Inheritance (Contd). Using inheritance. Constructor?. ** ‘IS-A’ relationship. Using inheritance. define one superclass : Item define subclasses: Video and CD the superclass defines common attributes the subclasses inherit the superclass attributes - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 19

Lecture 19

- Inheritance (Contd)

Page 2: Lecture 19

Using inheritance

** ‘IS-A’ relationship

Constructor?

Page 3: Lecture 19

Using inheritance

• define one superclass : Item

• define subclasses: Video and CD

• the superclass defines common attributes

• the subclasses inherit the superclass attributes

• the subclasses add own attributes

Page 4: Lecture 19

Subtitution

First, we had: public void addCD(CD theCD) public void addVideo(Video theVideo)

Now, we have: public void addItem(Item theItem)

• Objects of subclasses can be used where objects of supertypes are required.(This is called substitution .)

Page 5: Lecture 19

public class Database{ private ArrayList items;

/** * Construct an empty Database. */ public Database() { items = new ArrayList(); }

/** * Add an item to the database. */ public void addItem(Item theItem) { items.add(theItem); } ...}

New Database

source code

avoids code duplication in client!

Page 6: Lecture 19

/** * Print a list of all currently stored CDs and * videos to the text terminal. */public void list(){ for(Iterator iter = items.iterator(); iter.hasNext(); ) { Item item = (Item)iter.next(); item.print(); System.out.println(); // empty line between items }}

New Database source code

Page 7: Lecture 19

Assignment

Item i1 = new Item();Item i2 = new CD();Item i3 = new Video();

// Illegal Assignments* CD c1 = new Item();CD c2 = new Video();

subclass objects may be assigned to superclass variables (Substitution)

Page 8: Lecture 19

Casting and Assignment

Item anItem1; //superclassCD cd1; //subclasscd1 = new CD ();anItem1 = cd1; // subtype object can be assigned to the supertype - substitution

Item anItem2;CD cd2;anItem = new CD();cd2 = (CD) anItem; // ok superclass object can be assigned to subtype with a cast

Video v1;CD cd3;cd3 = new CD();v1 = cd3; //error – compile time … (compilation error)Item anItem3 = cd3;v1 = (Video) anItem3; // will compile, but runtime error

Page 9: Lecture 19

DoMe Example (contd)

Page 10: Lecture 19

Using inheritance

Page 11: Lecture 19

The problem

• The print method in Item only prints the common fields.

• Inheritance is a one-way street:– A subclass inherits the superclass fields.– The superclass knows nothing about its

subclass’s fields.

Page 12: Lecture 19

Attempting to solve the problem

• Place print where it has access to the information it needs.

• Each subclass has its own version.

• But Item’s fields are private.

Page 13: Lecture 19

/** * Print a list of all currently stored CDs and * videos to the text terminal. */public void list(){ for(Iterator iter = items.iterator(); iter.hasNext(); ) { Item item = (Item)iter.next(); item.print(); System.out.println(); // empty line between items }}

New Database source code

No print method in Item: Code won’t compile

Page 14: Lecture 19

Static type and dynamic type

• A more complex type hierarchy requires further concepts to describe it.

• Some new terminology:– static type– dynamic type– method dispatch/lookup

Page 15: Lecture 19

Static and dynamic type

• The declared type of a variable is its static type.

• The type of the object a variable refers to is its dynamic type.

• The compiler’s job is to check for static-type violations.

Page 16: Lecture 19

Overriding: the solution

print method in both super- and subclasses.

Satisfies both static

and dynamic

type checking.

Page 17: Lecture 19

Overriding

• Superclass and subclass define methods with the same signature.

• Each has access to the fields of its class.

• Superclass satisfies static type check.• Subclass method is called at runtime

– it overrides the superclass version.• What becomes of the superclass

version?

Page 18: Lecture 19

Method lookup

No inheritance or polymorphism.The obvious method is selected.

Page 19: Lecture 19

Method lookup

Inheritance but no overriding. The

inheritance hierarchy is ascended, searching for

a match.

Page 20: Lecture 19

Method lookup

Polymorphism and overriding. The

‘first’ version found is used.

Page 21: Lecture 19

Method lookup summary

• The object stored in the variable is found.• The class of the object is found.• The class is searched for a method match.• If no match is found, the superclass is searched.• This is repeated until a match is found, or the

class hierarchy is exhausted.• Overriding methods take precedence.

Page 22: Lecture 19

Super call in methods

• Overridden methods are hidden ...• ... but we often still want to be able to call

them.• An overridden method can be called from

the method that overrides it.– super.method(...)– Compare with the use of super in

constructors.

Page 23: Lecture 19

Calling an overridden method

public class CD{ ... public void print() { super.print(); System.out.println(" " + artist); System.out.println(" tracks: " + numberOfTracks); } ...}

Page 24: Lecture 19

Method polymorphism

• We have been discussing polymorphic method dispatch.

• A polymorphic variable can store objects of varying types.

• Method calls are polymorphic.– The actual method called depends on the

dynamic object type.

Page 25: Lecture 19

Review

• The declared type of a variable is its static type.– Compilers check static types.

• The type of an object is its dynamic type.– Dynamic types are used at runtime.

• Methods may be overridden in a subclass.• Method lookup starts with the dynamic type.• Protected access supports inheritance.