Java Reference
In-Depth Information
CHAPTER
9
More about inheritance
Main concepts discussed in this chapter:
■
method polymorphism
■
overriding
■
static and dynamic type
■
dynamic method lookup
Java constructs discussed in this chapter:
super
(in method),
toString
,
protected
,
instanceof
The last chapter introduced the main concepts of inheritance by discussing the
network
example. While we have seen the foundations of inheritance, there are still numerous important
details that we have not yet investigated. Inheritance is central to understanding and using
object-oriented languages, and understanding it in detail is necessary to progress from here.
In this chapter, we shall continue to use the
network
example to explore the most important of
the remaining issues surrounding inheritance and polymorphism.
9.1
The problem:
network's
display method
When you experimented with the
network
examples in Chapter 8, you probably noticed that
the second version—the one using inheritance—has a problem: the
display
method does not
show all of a post's data.
Let us look at an example. Assume that we create a
MessagePost
and a
PhotoPost
object
with the following data:
The message post:
Leonardo da Vinci
Had a great idea this morning.
But now I forgot what it was. Something to do with flying . . .
40 seconds ago. 2 people like this.
No comments.
Search WWH ::
Custom Search