Java Reference
In-Depth Information
Figure 9.1
Display, version 1:
display
method in
superclass
NewsFeed
Post
GLVSOD\
MessagePost
PhotoPost
9.2
Static type and dynamic type
Trying to solve the problem of developing a complete polymorphic
display
method leads us
into a discussion of
static
and
dynamic types
and
method lookup.
But let us start at the beginning.
A first attempt to solve the display problem might be to move the
display
method to the sub-
classes (Figure 9.2). That way, because the method would now belong to the
MessagePost
and
PhotoPost
classes, it could access the specific fields of
MessagePost
and
PhotoPost
.
It could also access the inherited fields by calling accessor methods defined in the
Post
class.
That should enable it to display a complete set of information again. Try out this approach by
completing Exercise 9.1.
Figure 9.2
Display, version 2:
display
method
in subclasses
NewsFeed
Post
MessagePost
PhotoPost
GLVSOD\
GLVSOD\
Search WWH ::
Custom Search