Java Reference
In-Depth Information
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 13.4.
multiple inheritance
single inheritance
11.1
True or false? A subclass is a subset of a superclass.
Check
11.2
Point
What keyword do you use to define a subclass?
11.3
What is single inheritance? What is multiple inheritance? Does Java support multiple
inheritance?
11.3 Using the super Keyword
The keyword super refers to the superclass and can be used to invoke the super-
class's methods and constructors.
A subclass inherits accessible data fields and methods from its superclass. Does it inherit
constructors? Can the superclass's constructors be invoked from a subclass? This section
addresses these questions and their ramifications.
Section 9.14, 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:
Key
Point
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 .
The syntax to call a superclass's constructor is:
super (), or super (parameters);
The statement super() invokes the no-arg constructor of its superclass, and the statement
super(arguments) invokes the superclass constructor that matches the arguments . The
statement super() or super(arguments) must be the first statement of the subclass's con-
structor; this is the only way to explicitly invoke a superclass constructor. For example, the
constructor in lines 12-17 in Listing 11.2 can be replaced by the following code:
public CircleFromSimpleGeometricObject(
double radius, String color, boolean filled) {
 
 
 
Search WWH ::




Custom Search