Java Reference
In-Depth Information
You can define a data member in a derived class with the same name as a data member in the base class.
This is not a recommended approach to class design generally, but it's possible that it can arise unintention-
ally. When it occurs, the base class data member may still be inherited, but are hidden by the derived class
member with the same name. The hiding mechanism applies regardless of whether the respective types or
access attributes are the same or not — the base class member is hidden in the derived class if the names are
the same.
Any use of the derived class member name always refers to the member defined as part of the derived
class. To refer to the inherited base class member, you must qualify it with the keyword super to indicate
it is the member of the superclass that you want. Suppose you have a data member value as a member of
the base class, and a data member with the same name in the derived class. In the derived class, the name
value references the derived class member, and the name super.value refers to the member inherited from
the base class. Note that you cannot use super.super.something to refer to a member name hidden in the
base class of a base class.
In most situations you won't need to refer to inherited data members in this way, as you would not delib-
erately set out to use duplicate names. The situation can commonly arise if you are using a class as a base
that is subsequently modified by adding data members — it could be a Java library class, for example, or
some other class in a package designed and maintained by someone else. Because your code does not pre-
sume the existence of the base class member with the same name as your derived class data member, hiding
the inherited member is precisely what you want. It enables the base class to be altered without breaking
your code.
Inherited Methods
Ordinary methods in a base class, by which I mean methods that are not constructors, are inherited in a de-
rived class in the same way as the data members of the base class. Those methods declared as private in a
base class are not inherited, and those that you declare without an access attribute are inherited only if you
define the derived class in the same package as the base class. The rest are all inherited.
Constructors are different from ordinary methods. Constructors in the base class are never inherited, re-
gardless of their attributes. You can look into the intricacies of constructors in a class hierarchy by consider-
ing how derived class objects are created.
Objects of a Derived Class
I said at the beginning of this chapter that a derived class extends a base class. This is not just jargon — it
really does do this. As I have said several times, inheritance is about what members of the base class are
accessible in a derived class, not what members of the base class exist in a derived class object. An object of
a subclass contains all the members of the original base class, plus any new members that you have defined
in the derived class. This is illustrated in Figure 6-3 .
FIGURE 6-3
 
 
Search WWH ::




Custom Search