Java Reference
In-Depth Information
A Spaniel object is a specialized instance of a Dog object. This reflects real life. A spaniel is obviously
a dog and will have all the properties of a basic dog, but it has some unique characteristics of its own
which distinguish it from all the dogs that are not spaniels. The inheritance mechanism that adds all the
properties of the base class - Dog in this instance - to those in the derived class, is a good model for the
real world. The members of the derived class define the properties that differentiate it from the base
type, so when you derive one class from another, you can think of your derived class as a specification
for objects that are specializations of the base class object.
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 derived
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 our Spaniel class, derived from Dog , the data
members to hold the name and the breed, that are defined for the class Dog , would automatically be in the
class Spaniel . A Spaniel object will always have 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 member 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
method in the class. Objects of the derived class type will 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. Note that a
derived class object always contains a complete base class object within it, including all the fields and
methods that are not inherited. We need 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.
We need to consider several aspects of defining and using a derived class. First of all we need to know
which members of the base class are inherited in the derived class. We will look at what this implies for
data members and methods separately - there are some subtleties here we should be quite clear on. We
will 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
The next diagram 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.
Search WWH ::




Custom Search