Base Classes and Derived Classes

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 is incorrect to claim that every quadrilateral is a rectanglethe quadrilateral could be a parallelogram or some other shape. Figure 10.1 lists several simple examples of base classes and derived classesnote that base classes tend to be "more general," and derived classes tend to be "more specific."

Figure 10.1. Inheritance examples.

Base class

Derived classes

Student

GraduateStudent, UndergraduateStudent

Shape

Circle, TRiangle, Rectangle

Loan

CarLoan, HomeImprovementLoan, MortgageLoan

Employee

Faculty, Staff, HourlyWorker, CommissionWorker

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 vehiclescars, trucks, boats, bicycles and so on. By contrast, derived class Car represents a smaller, more specific subset of vehicles.

Inheritance relationships form tree-like hierarchical structures (Figs. 10.2 and 10.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. In some cases, a class is both a base class and a derived class.

Figure 10.2. UML class diagram showing an inheritance hierarchy for university CommunityMembers.

Figure 10.3. UML class diagram showing an inheritance hierarchy for Shapes.

(This item is displayed on page 468 in the print version)

Let us develop a sample class hierarchy, also called an inheritance hierarchy (Fig. 10.2). The UML class diagram of Fig. 10.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. Note that 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 in this class hierarchy, 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 of the diagram, the reader 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 inheritance hierarchy in Fig. 10.3. This hierarchy begins with base class Shape, which is extended by derived classes TwoDimensionalShape and ThreeDimensionalShapeShapes are either TwoDimensionalShapes or THReeDimensionalShapes. The third level of this hierarchy contains some specific TwoDimensionalShapes and ThreeDimensionalShapes. As in Fig. 10.2, we can follow the arrows from the bottom of the class diagram to the topmost base class in this class hierarchy to identify several is-a relationships. For instance, a TRiangle is a TwoDimensionalShape and is a Shape, while a Sphere is a THReeDimensionalShape and is a Shape. Note that this hierarchy could contain many other classes. For example, ellipses and trapezoids are TwoDimensionalShapes.

Not every class relationship is an inheritance relationship. In Chapter 9, 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 is 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 is possible to treat base class objects and derived class objects similarlytheir commonalities are expressed in the members of the base class. Objects of all classes that extend a common base class can be treated as objects of that base class (i.e., 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 (the other vehicles could be trucks, planes or bicycles, for example). In this chapter and in Chapter 11, Polymorphism, Interfaces & Operator Overloading, we consider many examples of is-a relationships.

One problem with inheritance is that a derived class can inherit methods that it does not need or should not have. Even when a base class method is appropriate for a derived class, that derived class often needs a customized version of the method. In such cases, the derived class can override (redefine) the base class method with an appropriate implementation, as we will see often in the chapter's code examples.

Категории