Java Reference
In-Depth Information
Note the following points regarding inheritance:
Contrary to the conventional interpretation, a subclass is not a subset of its super-
class. In fact, a subclass usually contains more information and methods than its
superclass.
more in subclass
Private data fields in a superclass are not accessible outside the class. Therefore, they
cannot be used directly in a subclass. They can, however, be accessed/mutated
through public accessors/mutators if defined in the superclass.
private data fields
Not all is-a relationships should be modeled using inheritance. For example, a square
is a rectangle, but you should not extend a Square class from a Rectangle class,
because the width and height properties are not appropriate for a square. Instead,
you should define a Square class to extend the GeometricObject class and define
the side property for the side of a square.
nonextensible is-a
Inheritance is used to model the is-a relationship. Do not blindly extend a class just
for the sake of reusing methods. For example, it makes no sense for a Tree class to
extend a Person class, even though they share common properties such as height
and weight. A subclass and its superclass must have the is-a relationship.
no blind extension
Some programming languages allow you to derive a subclass from several classes.
This capability is known as multiple inheritance . Java, however, does not allow mul-
tiple inheritance. A Java class may inherit directly from only one superclass. This
restriction is known as single inheritance . If you use the extends keyword to define
a subclass, it allows only one parent class. Nevertheless, multiple inheritance can be
achieved through interfaces, which will be introduced in Section 15.4.
multiple inheritance
single inheritance
11.1 True or false? A subclass is a subset of a superclass.
11.2 What keyword do you use to define a subclass?
11.3 What is single inheritance? What is multiple inheritance? Does Java support multiple
inheritance?
Check
Point
11.3 Using the super Keyword
The keyword super refers to the superclass and can be used to invoke the superclass's
methods and constructors.
Key
Point
A subclass inherits accessible data fields and methods from its superclass. Does it inherit con-
structors? Can the superclass's constructors be invoked from a subclass? This section
addresses these questions and their ramifications.
Section 10.4, The this Reference, introduced the use of the keyword this to reference
the calling object. The keyword super refers to the superclass of the class in which super
appears. It can be used in two ways:
To call a superclass constructor.
To call a superclass method.
11.3.1 Calling Superclass Constructors
A constructor is used to construct an instance of a class. Unlike properties and methods, the
constructors of a superclass are not inherited by a subclass. They can only be invoked from
the constructors of the subclasses using the keyword super .
 
 
Search WWH ::




Custom Search