Chapter 9

advertisement
More about inheritance
(There'ss more?)
(There
Exploring polymorphism
3.0
Main concepts to be covered
•
•
•
•
•
method polymorphism
static and dynamic
y
type
yp
overriding
dynamic method lookup
protected access
2
The inheritance hierarchy
3
Conflicting output
Wh we want
What
CD: A Swingin' Affair (64 mins)*
Frank Sinatra
tracks: 16
my favourite Sinatra album
DVD: O Brother, Where Art Thou? (106 mins)
Joel & Ethan Coen
The Coen brothers’ best movie!
What we have
title: A Swingin' Affair (64 mins)*
my favourite Sinatra album
title: O Brother, Where Art Thou? (106 mins)
The Coen brothers’ best movie!
4
The problem
• Th
The print method
h d iin Item only
l
prints the common fields.
• Inheritance is a one-way street:
– A subclass inherits the superclass
p
fields.
– The superclass knows nothing about its
subclass’s fields.
5
Attempting to solve the
problem
Heads I win,
win tails you lose!
• Pl
Place print
i where
h
it
has access to the
information it needs
(in CD and DVD)
• Each subclass has its
own version.
version
• But Item’s fields are
private. Darn!
p
• But Database cannot
find a print method
in Item
It since it's
it s not
there! Darn!
6
Static type and dynamic type
• A more complex type hierarchy
requires further concepts to describe
it.
• Some new terminology:
– static type
y
type
yp
– dynamic
– method dispatch/lookup
7
Static and dynamic type
What is the type of c1?
What is the type of v1?
Car c1 = new Car();
Vehicle v1 = new Car();
8
Static and dynamic type
• Th
The declared
d l d type
t
off a variable
i bl is
i it
its
static type. What you typed.
• The
Th ttype off th
the object
bj t a variable
i bl
refers to is its dynamic type. What it
is.
is
• The compiler’s job is to check for
static type violations.
static-type
violations
for(Item item : items) {
item.print(); // Compile-time
Compile time error.
} // The Item static type has no print method.
9
Overriding: the solution
Put one here to
say there is one!
print method
in both superand
d subclasses.
b l
Satisfies both
static and
dynamic type
checking.
But will use this one!
10
Overriding
• Superclass and subclass define
methods with the same signature.
• Each has access to the fields of its
class.
• Superclass satisfies static type check.
check
• Subclass method is called at runtime
version
– it overrides the superclass version.
• What becomes of the superclass
version?
11
Method lookup
No inheritance or polymorphism.
The obvious method is selected.
selected
12
Method lookup
Inheritance but no
overriding. The inheritance
hierarchy is ascended,
searching
hi for
f a match.
h
13
Method lookup
Polymorphism and
overriding. The ‘first’
version found is used.
used
14
Method lookup summary
The variable is accessed.
Th object
The
bj t stored
t d iin th
the variable
i bl iis ffound.
d
The class of the object is found.
Th class
The
l
iis searched
h d ffor a method
th d match.
t h
If no match is found, the superclass is
searched.
searched
• This is repeated until a match is found, or
the class hierarchyy is exhausted.
• Overriding methods take precedence. If
you write it, you use it. Classic:
t St i g()
toString()
•
•
•
•
•
15
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. And (protected) variables
in superclass.
16
Calling an overridden method
public class CD
{
...
public void print()
{
super.print();
System.out.println("
" + artist);
System.out.println("
tracks: " +
numberOfTracks);
}
...
}
17
Method polymorphism
• We have been discussing polymorphic
method dispatch.
• A polymorphic variable can store
j
of varying
y g types.
yp
objects
• Method calls are polymorphic.
– The actual method called depends on
the dynamic object type.
18
The Object class
class’ss methods
• Methods in Object are inherited by
all classes.
• Any of these may be overridden.
• The toString method is commonly
overridden:
– public
bli St
String
i
t
toString()
St i ()
– Returns a string representation of the
object
object.
19
Overriding toString
public class Item
{
...
public String toString()
{
String line1 = title +
" (" + playingTime + " mins)");
if(gotIt) {
return line1 + "*\n" + "
" +
comment + "\n");
} else {
return line1 + "\n" + "
" +
comment + "\n");
}
}
...
}
20
Overriding toString
• E
Explicit
li i print methods
h d can often
f
be
b
omitted from a class:
– System.out.println(item.toString());
• Calls to println with just an object
automatically
t
ti ll result
lt iin toString
i
being called:
– System.out.println(item);
21
Protected access
• Private access in the superclass may be too
restrictive for a subclass.
(default) access given only to classes in the
same package.
• The closer inheritance relationship is
supported by protected access.
• Protected access is more restricted than
public access. Subclasses can access (as
well as classes in the same package.)
package )
We still recommend keeping fields private.
– Define protected accessors and mutators.
mutators
22
Access levels
23
Review
• Th
The declared
d l d type
t
off a variable
i bl iis it
its static
t ti
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.
24
Download