Java Reference
In-Depth Information
Because an inherited variable is already available to the child class, there is
usually no good reason to redeclare it. Someone reading code with a shadowed
variable will find two different declarations that seem to apply to a variable used
in the child class. This confusion causes problems and serves no useful purpose.
A redeclaration of a particular variable name could change its type, but that is
usually unnecessary. In general, shadowing variables should be avoided.
SELF-REVIEW QUESTIONS (see answers in Appendix N)
SR 9.8
Why would a child class override one or more of the methods of its
parent class?
SR 9.9
True or False? Explain.
a. A child class may define a method with the same name as a
method in the parent class.
b. A child class can override the constructor of the parent class.
c. A child class can override a final method of the parent class.
d. It is considered poor design when a child class overrides a method
from the parent class.
e. A child class may define a variable with the same name as a vari-
able in the parent class.
9.3 Class Hierarchies
A child class derived from one parent can be the parent of its own
child class. Furthermore, multiple classes can be derived from a
single parent. Therefore, inheritance relationships often develop
into class hierarchies. The diagram in Figure 9.3 shows a class
KEY CONCEPT
The child of one class can be the
parent of one or more other classes,
creating a class hierarchy.
Animal
Reptile
Bird
Mammal
Snake
Lizard
Parrot
Horse
Bat
FIGURE 9.3 A UML class diagram showing a class hierarchy
 
Search WWH ::




Custom Search