Java Reference
In-Depth Information
The photo post:
Alexander Graham Bell
I think I might call this thing 'telephone'.
[experiment.jpg]
12 minutes ago. 4 people like this.
No comments.
If we enter these objects into the news feed
1
and then invoke the first version of the news feed's
show
method (the one without inheritance), it prints
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.
Alexander Graham Bell
[experiment.jpg]
I think I might call this thing 'telephone'.
12 minutes ago - 4 people like this.
No comments.
While the formatting isn't pretty (because, in the text terminal, we don't have format-
ting options available), all the information is there, and we can imagine how the
show
method might be adapted later to show the data in a nicer formatting in a different user
interface.
Compare this with the second
network
version (with inheritance), which prints only
Leonardo da Vinci
40 seconds ago - 2 people like this.
No comments.
Alexander Graham Bell
12 minutes ago - 4 people like this.
No comments.
We note that the message post's text, as well as the photo post's image filename and
caption, are missing. The reason for this is simple. The
display
method in this version
is implemented in the
Post
class, not in
MessagePost
and
PhotoPost
(Figure 9.1).
In the methods of
Post
, only the fields declared in
Post
are available. If we tried to
access the
MessagePost
's
message
field from
Post
's
display
method, an error would
be reported. This illustrates the important principle that inheritance is a one-way street:
MessagePost
inherits the fields of
Post
, but
Post
still does not know anything about fields
in its subclasses.
1
The text for the message post is a two-line string. You can enter a multiline text into a string by using
ā\nā in the string for the line break.
Search WWH ::
Custom Search