Inheritance enables you to define a new class based on a class that already exists. The new class will inherit the characteristics of the existing class, but may also provide some additional capabilities. This makes programming easier, because you can reuse and extend your previous work and avoid duplication of code.
The class that is used as a basis for defining a new class is called a superclass (or parent class or base class). The new class based on the superclass is called a subclass (or child class or derived class.)
The process by which a subclass inherits characteristics from just one parent class is called single inheritance. Some languages allow a derived class to inherit from more than one parent class in a process called multiple inheritance. Multiple inheritance makes it difficult to determine which class will contribute what characteristics to the child class. Java avoids these issues by only providing support for single inheritance.
Figure 11.1 shows a superclass and a subclass. The line between them shows the "is a" relationship. The picture can be read as "a Student is a Person." The clouds represent the classes. That is, the picture does not show any particular Student or any particular Person, but shows that the class Student is a subclass of the Person class.
Inheritance is between classes, not between objects. A superclass is a blueprint that is followed when a new object is constructed. That newly constructed object is another blueprint that looks much like the original, but with added features. The subclass in turn can be used to construct objects that look like the superclass’s objects, but with additional capabilities.
Figure 11.2 shows a superclass and a subclass, and some objects that have been constructed from each. These objects that are shown as rectangles are actual instances of the class. In the picture, Albert Einstein, Lynne Brooke, and Monty Vista represent actual objects.
Last modified: December 12, 2022