Java Reference
In-Depth Information
Hiding Data Members
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 is possible that it can arise
unintentionally. When it occurs, the base class data member may still be inherited, but will be 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 will be hidden in
the derived class if the names are the same.
Any use of the derived class member name will always refer 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
deliberately 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
instance, or some other class in a package designed and maintained by someone else. Since your code
did not presume 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 allows 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
derived 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 only inherited 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,
regardless of their attributes. We can look into the intricacies of constructors in a class hierarchy by
considering how derived class objects are created.
Objects of a Derived Class
We 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 we 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 will contain all the members of the original base class, plus any new
members defined in the derived class (see following diagram).
Search WWH ::




Custom Search