Often, an object of one class is an object of another class as well. For example, in geometry, a rectangle is a quadrilateral (as are squares, parallelograms and trapezoids). Thus, class Rectangle
can be said to inherit from class Quadrilateral
. In this context, class Quadrilateral
is a base class and class Rectangle
is a derived class. A rectangle is a specific type of quadrilateral, but it’s incorrect to claim that every quadrilateral is a rectangle—the quadrilateral could be a parallelogram or some other shape. Figure 11.1 lists several simple examples of base classes and derived classes—base classes tend to be more general, and derived classes tend to be more specific.
Base class | Derived classes |
---|---|
Student |
GraduateStudent , UndergraduateStudent |
Shape |
Circle , Triangle , Rectangle |
Loan |
CarLoan , HomeImprovementLoan , MortgageLoan |
Employee |
Faculty , Staff , HourlyWorker , CommissionWorker |
SpaceObject |
Star , Moon , Planet , FlyingSaucer |
BankAccount |
CheckingAccount , SavingsAccount |
Because every derived-class object is an object of its base class, and one base class can have many derived classes, the set of objects represented by a base class is typically larger than the set of objects represented by any of its derived classes. For example, the base class Vehicle
represents all vehicles—cars, trucks, boats, bicycles and so on. By contrast, derived class Car
represents a smaller, more specific subset of vehicles.
Inheritance relationships form treelike hierarchical structures (Figs. 11.2 and 11.3). A base class exists in a hierarchical relationship with its derived classes. When classes participate in inheritance relationships, they become “affiliated” with other classes. A class becomes either a base class, supplying members to other classes, or a derived class, inheriting its members from another class. Sometimes, a class is both a base and a derived class.
Let us develop a sample class hierarchy, also called an inheritance hierarchy (Fig. 11.2). The UML class diagram of Fig. 11.2 shows a university community that has many types of members, including employees, students and alumni. Employees are either faculty members or staff members. Faculty members are either administrators (such as deans and department chairpersons) or teachers. The hierarchy could contain many other
classes. For example, students can be graduate or undergraduate students. Undergraduate students can be freshmen, sophomores, juniors or seniors.
Each arrow with a hollow triangular arrowhead in the hierarchy diagram represents an is-a relationship. As we follow the arrows, we can state, for instance, that “an Employee
is a CommunityMember
” and “a Teacher
is a Faculty
member.” CommunityMember
is the direct base class of Employee
, Student
and Alumnus
and is an indirect base class of all the other classes in the diagram. Starting from the bottom, you can follow the arrows and apply the is-a relationship up to the topmost base class. For example, an Administrator
is a Faculty
member, is an Employee
and is a CommunityMember
.
Now consider the Shape
hierarchy in Fig. 11.3, which begins with base class Shape
. This class is extended by derived classes TwoDimensionalShape
and ThreeDimensional-Shape
—a Shape
is either a TwoDimensionalShape
or a ThreeDimensionalShape
. The third level of this hierarchy contains specific TwoDimensionalShape
s and ThreeDimensional-Shape
s. We can follow the arrows from the bottom to the topmost base class in this hierarchy to identify the is-a relationships. For instance, a Triangle
is a TwoDimensionalShape
and is a Shape
, while a Sphere
is a ThreeDimensionalShape
and is a Shape
. This hierarchy could contain many other classes. For example, ellipses and trapezoids also are TwoDimensionalShape
s.
Not every class relationship is an inheritance relationship. In Chapter 10 we discussed the has-a relationship, in which classes have members that are references to objects of other classes. Such relationships create classes by composition of existing classes. For example, given the classes Employee
, BirthDate
and TelephoneNumber
, it’s improper to say that an Employee
is a BirthDate
or that an Employee
is a TelephoneNumber
. However, an Employee
has a BirthDate
, and an Employee
has a TelephoneNumber
.
It’s possible to treat base-class objects and derived-class objects similarly—their commonalities are expressed in the base class’s members. Objects of all classes that extend a common base class can be treated as objects of that base class—such objects have an is-a relationship with the base class. However, base-class objects cannot be treated as objects of their derived classes. For example, all cars are vehicles, but not all vehicles are cars (other vehicles could be trucks, planes, bicycles, etc.). This chapter and Chapter 12 consider many examples of is-a relationships.
A derived class can customize methods it inherits from its base class. In such cases, the derived class can override (redefine) the base-class method with an appropriate implementation, as we’ll see often in the chapter’s code examples.