objects first with java a practical introduction using bluej method overriding 2.0

12
Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0

Upload: dinah-hancock

Post on 18-Jan-2016

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0

Objects First With JavaA Practical Introduction Using BlueJ

Method overriding

2.0

Page 2: Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0

2

media-v2

Page 3: Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0

3

Conflicting outputCD: A Swingin' Affair (64 mins)* My favourite Sinatra album Frank Sinatra Tracks: 16 DVD: O Brother, Where Art Thou? (106 mins) The Coen brothers’ best movie! Joel & Ethan Coen

title: A Swingin' Affair (64 mins)* My favourite Sinatra album

title: O Brother, Where Art Thou? (106 mins) The Coen brothers’ best movie!

What we want

What we now have

Page 4: Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0

4

The problem

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

• Inheritance is a one-way street:– The superclass knows nothing about

its subclass’s fields.

Page 5: Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0

5

The solution: overriding• Superclasses and their

subclasses can define methods with the same name.

• We shall see how this can be used.

Page 6: Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0

6

Method lookup

No inheritance.The method is selected.

Page 7: Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0

7

Method lookup

Inheritance but no overriding. The inheritance hierarchy is ascended, searching for a match.

Page 8: Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0

8

Method lookup

Overriding. The Subclass method overrides the superclass version.

Page 9: Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0

9

Method lookup summary

• The variable is accessed.• 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.• Overriding methods take precedence.

Page 10: Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0

10

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.methodName(...)– Compare with the use of super in

constructors.

Page 11: Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0

11

Calling an overridden method

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

Page 12: Objects First With Java A Practical Introduction Using BlueJ Method overriding 2.0

12

Review

• Methods can be overridden in a subclass

• Overriding methods take precedence

• Superclass methods can be called using the keyword super