|
Polymorphism through Inheritance
With Examples in Java |
|
Prof. David Bernstein
|
| Computer Science Department |
| bernstdh@jmu.edu |
public class Chirp {...}
public class ExpandedChirp
extends Chirp {...}
public void addChirp(Chirp ch) {...}
Chirp object to the
addChirp() method?ExpandedChirp object
to the addChirp() method?
ch
Chirp object to the
addChirp() method (since the
formal parameter is declared to be a Chirp)ExpandedChirp object to the
addChirp() method (since
the ExpandedChirp class inherits the accessible
attributes from the Chirp class)
public class Chirp {...}
public class ExpandedChirp
extends Chirp {...}
Chirp has a getText() methodExpandedChirp overrides the
getText() methodaddChirp() is passed a Chirp
object and calls the getText()
method?addChirp() is passed an
ExpandedChirp
object and calls the getText()
method?
Since an ExpandedChirp "is a" Chirp, we
know that a Chirp[] (i.e., an array of references
to Chirp objects) can contain both.
What we need to know is what happens when an
element's getText() method is called.
getText() method
of the Chirp object is called
getText()
message is sent to the Chirp
object
What Memory Might Look Like After Constructing The
OverdraftAccount
Chirp Class:
getNumberOfWords() method uses the
protected String attribute
named text
getText() method insteadgetNumberOfWords() message is sent to
a Chirp() object?getNumberOfWords() message is sent to
an ExpandedChirp() object?
public class Chirp {...}
public class ExpandedChirp
extends Chirp {...}
public void add(Chirp ch) {...}
public void add(ExpandedChirp ch) {...}
add() is passed a Chirp
that is declared to be a Chirp?add(Chirp ch) is executed -- there is no other
option
add() is passed an ExpandedChirp
that is declared to be an ExpandedChirp?add(ExpandedChirp ch) is executed --
this is not a situation in which dynamic/late binding
is used at run-time, the declared type is used at compile-time
to determine which method will be executed
add() is passed an ExpandedChirp
that is declared to be a Chirp?add(Chirp ch) is executed --
again, the declared type is used at compile-time to determine
which method is executed
public class Chirp {...}
public class ExpandedChirp
extends Chirp {...}
public void add(Chirp ch) {...}
add() is passed an ExpandedChirp
that is declared to be an ExpandedChirp?add() compiles because
an ExpandedChirp is a Chirp
add(Chirp ch) is executed --
there is no other option
add() is passed an ExpandedChirp
that is declared to be a Chirp?add() compiles because
the actual parameter is declared to be the same type as
the formal parameter
add(Chirp ch) is executed --
there is no other option
public class Animal {...}
public class Mammal extends Animal {...}
public class Sheep extends Mammal {...}
public void add(Animal item) {...}
public void add(Mammal item) {...}
add() is passed an Animal
that is declared to be an Animal?add(Animal item) is executed -- there is
no other option
add() is passed a Mammal
that is declared to be a Mammal?add(Mammal item) is executed --
the declared type is used
add() is passed a Sheep
that is declared to be a Sheep?add(Mammal item) is executed --
there is no method matching the declared type but a
Sheep "is a" Mammal and a
Sheep "is a" Animal so
both methods could be executed; the most
specific/specialized version is executed