A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
Excepting Object
, which has no superclass, every class has one and only
one direct superclass (single inheritance).
In the absence of any other explicit
superclass, every class is implicitly a subclass of Object
.
Classes can be derived from classes that are derived from classes that are derived from classes, and so on,
and ultimately derived from the topmost class, Object
.
Such a class is said to be descended from all the classes in the inheritance chain stretching back to
Object
.
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
Object
class, defined in the java.lang
package, defines
and implements behavior common to all classes—including the ones that you write.
In the Java platform, many classes derive directly from Object
,
other classes derive from some of those classes, and so on, forming a
hierarchy of classes.
All Classes in the Java Platform are Descendants of Object
Object
is the most
general of all classes. Classes near the bottom of the hierarchy
provide more specialized behavior.
Bicycle
class that was
presented in
the Classes and Objects lesson:
public class Bicycle { // the Bicycle class has three fields public int cadence; public int gear; public int speed; // the Bicycle class has one constructor public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } // the Bicycle class has four methods public void setCadence(int newValue) { cadence = newValue; } public void setGear(int newValue) { gear = newValue; } public void applyBrake(int decrement) { speed -= decrement; } public void speedUp(int increment) { speed += increment; } }
A class declaration for a MountainBike
class that is a subclass of Bicycle
might look like this:
public class MountainBike extends Bicycle { // the MountainBike subclass adds one field public int seatHeight; // the MountainBike subclass has one constructor public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; } // the MountainBike subclass adds one method public void setHeight(int newValue) { seatHeight = newValue; } }
MountainBike
inherits all the fields and methods of Bicycle
and adds the field
seatHeight
and a method to set it. Except for the constructor, it is as if you had written a
new MountainBike
class entirely from scratch, with four fields and five methods. However, you didn't have to do
all the work. This would be especially valuable if the methods in the Bicycle
class were complex
and had taken substantial time to debug.
super
.
private
members of its parent class.
However, if the superclass has public or protected methods for accessing its private fields,
these can also be used by the subclass.
A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.
public MountainBike myBike = new MountainBike();
myBike
is of type MountainBike
.
MountainBike
is descended from
Bicycle
and Object
. Therefore,
a MountainBike
is a Bicycle
and is also an Object
,
and it can be used wherever Bicycle
or Object
objects are called for.
The reverse is not necessarily true: a Bicycle
may be a
MountainBike
, but it isn't necessarily. Similarly, an Object
may be a Bicycle
or a MountainBike
, but it isn't necessarily.
Casting shows the use of an object of one type in place of another type, among the objects permitted by inheritance and implementations. For example, if we write
Object obj = new MountainBike();
obj
is both an Object
and a Mountainbike
(until such
time as obj
is assigned another object that is not a Mountainbike
).
This is called implicit casting.
If, on the other hand, we write
MountainBike myBike = obj;
obj
is not known to the compiler
to be a MountainBike
.
However, we can tell the compiler that we promise to assign a MountainBike
to obj
by explicit casting:
MountainBike myBike = (MountainBike)obj;
obj
is assigned a MountainBike
so that the compiler
can safely assume that obj
is a MountainBike
.
If obj
is not a Mountainbike
at runtime, an exception will be thrown.
instanceof
operator. This can save you from a runtime error owing to an
improper cast. For example:
if (obj instanceof MountainBike) { MountainBike myBike = (MountainBike)obj; }
instanceof
operator verifies that obj
refers to a
MountainBike
so that we can make the cast with knowledge that there will
be no runtime exception thrown.