Java Reference
In-Depth Information
Self-Test Exercises
(continued)
12. Suppose
B
and
D
are as described in Self-Test Exercise 11. Is the following a legit-
imate method that can appear in the definition of the class
D
?
public void
demo2()
{
D object =
new
D();
object.doStuff();
//doStuff is inherited from B.
}
13. Suppose
B
and
D
are as described in Self-Test Exercise 11. Is the following a legit-
imate method that can appear in the definition of the class
D
?
public void
demo3()
{
B object =
new
B();
object.doStuff();
}
7.3
Programming with Inheritance
The devil is in the details.
COMMON SAYING
In the previous section, we described the basic idea and basic details about derived
classes. In this section, we continue that discussion and go on to discuss some more sub-
tle points about derived classes. In the process, we also discuss the class
Object
, which is
an ancestor class of all Java classes, and we describe a better way to define an
equals
method.
TIP: Static Variables Are Inherited
Static variables in a base class are inherited by any derived classes. The modifiers
public
,
private
, and
protected
, and package access have the same meaning for static variables
as they do for instance variables.
■