Java Reference
In-Depth Information
else
if (ageLevel <= 9)
return "Early";
else
if (ageLevel <= 12)
return "Middle";
else
return "Upper";
}
}
SR 9.7
With single inheritance, a class is derived from only one parent, whereas
with multiple inheritance, a class can be derived from multiple parents,
inheriting the properties of each. The problem with multiple inheritance
is that collisions must be resolved in the cases when two or more parents
contribute an attribute or method with the same name. Java supports
only single inheritance.
9.2 Overriding Methods
SR 9.8
A child class may prefer its own definition of a method in favor of the
definition provided for it by its parent. In this case, the child overrides
(redefines) the parent's definition with its own.
SR 9.9
The answers are:
a. True - The child “overrides” the parent's definition of the method
if both methods have the same signature.
b. False - A constructor is a special method with no return type
which has the same name as its class. If you tried to override the
parent's constructor, you would create a syntax error since all
methods except constructors must have a return type.
c. False - A final method cannot be overridden (that's why it is
“final”).
d. False - On the contrary, the need to override methods of a parent
class occurs often when using inheritance.
e. True - Such a variable is called a shadow variable. You can do this,
but it may lead to confusing situations and its use is discouraged.
9.3 Class Hierarchies
SR 9.10
There are many potential answers to this problem.
SR 9.11
All classes in Java are derived, directly or indirectly, from the Object
class. Therefore, all public methods of the Object class, such as equals
and toString , are available to every object.
Search WWH ::




Custom Search