Java Reference
In-Depth Information
put in the table will have the same second line as the
A
output, but we replace
"
A 1
"
with
"
C 1
"
and
"
A
"
with
"
C
"
. Table 9.5 shows the updated table.
Table 9.5
Method Output for Classes
A
,
B
, and
C
A
B
C
toString
A
A
C
method1
A 1
A 1
C 1
method2
A 2
B 2
A 2
The
D
class inherits all the behavior from
C
, except that it overrides the
method2
output to say
"
D 2
"
. The final output data are shown in Table 9.6.
Table 9.6
Method Output for Classes
A
,
B
,
C
, and
D
A
B
C
D
toString
A
A
C
C
method1
A 1
A 1
C 1
C 1
method2
A 2
B 2
A 2
D 2
Once you've created your table, you can find the output of the client code. The array
contains an
A
object, a
B
object, a
C
object, and a
D
object. For each of these it prints the
toString
output, then calls
method1
, then calls
method2
, then prints a blank line.
When a method gets called on an object, you can look up the output of that method for
that type in the table. The following is the complete output for the exercise:
A
A 1
A 2
A
A 1
B 2
C
C 1
A 2
C
C 1
D 2
In a more complicated version of the previous inheritance exercise, a class's methods
might call each other or interact with the superclass. Consider the following classes.
Search WWH ::
Custom Search