Java Reference
In-Depth Information
• A superclass's
private
members are hidden in its subclasses and can be accessed only through
the
public
or
protected
methods inherited from the superclass.
• An overridden superclass method can be accessed from a subclass if the superclass method name
is preceded by
super
(p. 364) and a dot (
.
) separator.
Section 9.4 Relationship Between Superclasses and Subclasses
• A subclass cannot access the
private
members of its superclass, but it can access the non-
private
members.
• A subclass can invoke a constructor of its superclass by using the keyword
super
, followed by a
set of parentheses containing the superclass constructor arguments. This must appear as the first
statement in the subclass constructor's body.
• A superclass method can be overridden in a subclass to declare an appropriate implementation
for the subclass.
•The
@Override
annotation (p. 369) indicates that a method should override a superclass method.
When the compiler encounters a method declared with
@Override
, it compares the method's sig-
nature with the superclass's method signatures. If there isn't an exact match, the compiler issues
an error message, such as “method does not override or implement a method from a supertype.”
•Method
toString
takes no arguments and returns a
String
. The
Object
class's
toString
method
is normally overridden by a subclass.
• When an object is output using the
%s
format specifier, the object's
toString
method is called
implicitly to obtain its
String
representation.
Section 9.5 Constructors in Subclasses
• The first task of a subclass constructor is to call its direct superclass's constructor (p. 378) to en-
sure that the instance variables inherited from the superclass are initialized.
Section 9.6 Class
Object
• See the table of class
Object
's methods in Fig. 9.12.
Self-Review Exercises
9.1
Fill in the blanks in each of the following statements:
a)
is a form of software reusability in which new classes acquire the members of
existing classes and embellish those classes with new capabilities.
b)
A superclass's
members can be accessed in the superclass declaration
and in
subclass declarations.
c)
In a(n)
relationship, an object of a subclass can also be treated as an object of
its superclass.
d)
In a(n)
relationship, a class object has references to objects of other classes as
members.
e)
In single inheritance, a class exists in a(n)
relationship with its subclasses.
f)
A superclass's members are accessible anywhere that the program has a refer-
ence to an object of that superclass or to an object of one of its subclasses.
g)
When an object of a subclass is instantiated, a superclass
is called implicitly or
explicitly.
h)
Subclass constructors can call superclass constructors via the
keyword.
9.2
State whether each of the following is
true
or
false
. If a statement is
false
, explain why.
a)
Superclass constructors are not inherited by subclasses.
b)
A
has-a
relationship is implemented via inheritance.