Java Reference
In-Depth Information
Chapter Summary
•
Inheritance
provides a tool for code reuse by deriving one class from another. The
derived class
automatically inherits the features of the old (base) class and may add
features as well.
•
A derived class object inherits the instance variables, static variables, and public
methods of the
base class
, and may add additional instance variables, static variables,
and methods.
•
An object of a derived class has the type of the derived class, and it also has the type
of the base class, and more generally, has the type of every one of its
ancestor classes
.
•
If an instance variable is marked
private
in a base class, then it cannot be accessed
by name in a derived class.
•
Private methods are effectively not inherited.
•
A method may be redefined in a derived class so that it performs differently from
how it performs in the base class. This is called
overriding
the method definition. The
definition for an overridden method is given in the class definition of the derived
class, in the same way as the definitions of any added methods.
•
A constructor of a base class can be used in the definition of a constructor for a
derived class. The keyword
super
is used as the name for a constructor of the base class.
•
A constructor definition can use the keyword
this
, as if it were a method name, to
invoke a constructor of the same class.
•
If a constructor does not contain an invocation of either
super
or
this
, then Java
automatically inserts an invocation of
super()
as the first action in the body of the
constructor definition.
•
A
protected
instance variable or method in the base class can be accessed by name
in the definition of a method of a derived class and in the definition of any method
in the same package.
•
If an instance variable or method has none of the modifiers
public
,
private
, or
protected
, then it is said to have
package access
. An instance variable or method
with package access can be accessed by name in the definition of any method in the
same package.
•
The class
Object
is an ancestor class of every class in Java.
•
The
equals
method for a class should have
Object
as the type of its one parameter.