Java Reference
In-Depth Information
of the base class object. Another way of thinking about this is that the base class defines a set of objects and
a derived class defines a specific subset of those that have particular defining characteristics.
CLASS INHERITANCE
In summary, when you derive a new class from a base class, the process is additive in terms of what makes
up a class definition. The additional members that you define in the new class establish what makes a de-
rived class object different from a base class object. Any members that you define in the new class are in
addition to those that are already members of the base class. For your Spaniel class that you derived from
Dog , the data members to hold the name and the breed that are defined for the class Dog are automatically in
the class Spaniel . A Spaniel object always has a complete Dog object inside it — with all its data members
and methods. This does not mean that all the members defined in the Dog class are available to methods that
are specific to the Spaniel class. Some are and some aren't. The inclusion of members of a base class in a
derived class so that they are accessible in that derived class is called class inheritance . An inherited mem-
ber of a base class is one that is accessible within the derived class. If a base class member is not accessible
in a derived class, then it is not an inherited member of the derived class, but base class members that are
not inherited still form part of a derived class object.
An inherited member of a derived class is a full member of that class and is freely accessible to any meth-
od in the class. Objects of the derived class type contain all the inherited members of the base class — both
fields and methods, as well as the members that are specific to the derived class. Remember that a derived
class object always contains a complete base class object within it, including all the fields and methods that
are not inherited. The next step is to take a closer look at how inheritance works and how the access attribute
of a base class member affects its visibility in a derived class.
You need to consider several aspects of defining and using a derived class. First of all, you need to know
which members of the base class are inherited in the derived class. I explain what this implies for data mem-
bers and methods separately — there are some subtleties here you need to be quite clear on. I also look at
what happens when you create an object of the derived class. There are some wrinkles in this context that
require closer consideration. Let's start by looking at the data members that are inherited from a base class.
Inheriting Data Members
Figure 6-2 shows which access attributes permit a class member to be inherited in a subclass. It shows what
happens when the subclass is defined in either the same package or a different package from that containing
the base class. Remember that inheritance implies accessibility of the member in a derived class, not just
presence.
FIGURE 6-2
 
 
Search WWH ::




Custom Search