Java Reference
In-Depth Information
A
method1
method2
toString
B
C
method2
method1
toString
D
method2
Figure 9.2
Hierarchy of classes A, B, C, and D
When someone calls method1 on an A object, the resulting output is " A 1 " . When
someone calls method2 on an A object, the resulting output is " A 2 " . When someone
prints an A object with toString , the resulting output is " A " . We can fill in the first
column of our table as shown in Table 9.3.
Table 9.3
Method Output for Class A
A
toString
A
method1
A 1
method2
A 2
The next layer in the hierarchy is the B class, which inherits all the behavior from
A , except that it overrides the method2 output to be " B 2 " . That means you can fill in
the B output on your table identically to the A output, except that you replace " A 2 "
with " B 2 " . Table 9.4 shows the table so far.
Table 9.4
Method Output for Classes A and B
A
B
toString
A
A
method1
A 1
A 1
method2
A 2
B 2
The C class also inherits all the behavior from A , but it overrides the method1 out-
put to be " C 1 " and it overrides the toString method to return " C " . Thus, the C out-
 
 
Search WWH ::




Custom Search