In the preceding lessons, you have seen inheritance mentioned several times. In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes.The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.
Definitions: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 ofObject.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 toObject.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.
The Java Platform Class Hierarchy
TheObjectclass, defined in thejava.langpackage, defines and implements behavior common to all classes—including the ones that you write. In the Java platform, many classes derive directly fromObject, other classes derive from some of those classes, and so on, forming a hierarchy of classes.At the top of the hierarchy,
All Classes in the Java Platform are Descendants of Object
Objectis the most general of all classes. Classes near the bottom of the hierarchy provide more specialized behavior.An Example of Inheritance
Here is the sample code for a possible implementation of aBicycleclass 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
MountainBikeclass that is a subclass ofBicyclemight 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; } }MountainBikeinherits all the fields and methods ofBicycleand adds the fieldseatHeightand a method to set it. Except for the constructor, it is as if you had written a newMountainBikeclass 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 theBicycleclass were complex and had taken substantial time to debug.What You Can Do in a Subclass
A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members:The following sections in this lesson will expand on these topics.
- The inherited fields can be used directly, just like any other fields.
- You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).
- You can declare new fields in the subclass that are not in the superclass.
- The inherited methods can be used directly as they are.
- You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
- You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
- You can declare new methods in the subclass that are not in the superclass.
- You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword
super.Private Members in a Superclass
A subclass does not inherit theprivatemembers 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.
Casting Objects
We have seen that an object is of the data type of the class from which it was instantiated. For example, if we writethenpublic MountainBike myBike = new MountainBike();myBikeis of typeMountainBike.
MountainBikeis descended fromBicycleandObject. Therefore, aMountainBikeis aBicycleand is also anObject, and it can be used whereverBicycleorObjectobjects are called for.The reverse is not necessarily true: a
Bicyclemay be aMountainBike, but it isn't necessarily. Similarly, anObjectmay be aBicycleor aMountainBike, 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
thenObject obj = new MountainBike();objis both anObjectand aMountainbike(until such time asobjis assigned another object that is not aMountainbike). This is called implicit casting.If, on the other hand, we write
we would get a compile-time error becauseMountainBike myBike = obj;objis not known to the compiler to be aMountainBike. However, we can tell the compiler that we promise to assign aMountainBiketoobjby explicit casting:This cast inserts a runtime check thatMountainBike myBike = (MountainBike)obj;objis assigned aMountainBikeso that the compiler can safely assume thatobjis aMountainBike. Ifobjis not aMountainbikeat runtime, an exception will be thrown.
Note: You can make a logical test as to the type of a particular object using theinstanceofoperator. This can save you from a runtime error owing to an improper cast. For example:Here theif (obj instanceof MountainBike) { MountainBike myBike = (MountainBike)obj; }instanceofoperator verifies thatobjrefers to aMountainBikeso that we can make the cast with knowledge that there will be no runtime exception thrown.