Section 10.2. Base Classes and Derived Classes


10.2. 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, in Visual Basic, 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

BankAccount

CheckingAccount, SavingsAccount


Every derived class object is an object of its base class, and one base class can have many derived classes, so 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, including cars, 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. 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 other classes. In some cases, a class is both a base class and a derived class.

CommunityMember Inheritance Hierarchy

Let's develop a sample class hierarchy (Fig. 10.2), also called an inheritance hierarchy. A university community has thousands 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.

Figure 10.2. Inheritance hierarchy for university CommunityMembers.


Each arrow in the inheritance hierarchy diagram of Fig. 10.2 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, 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.

Shape Inheritance Hierarchy

Now consider the Shape inheritance hierarchy in Fig. 10.3. This hierarchy begins with base class Shape, which is inherited by derived classes TwoDimensionalShape and ThreeDimensionalShapeShapes are either TwoDimensionalShapes or THReeDimensionalShapes. The third level of the hierarchy contains some more specific types of TwoDimensionalShapes and THReeDimensionalShapes. As in Fig. 10.2, we can follow the arrows from the bottom of the diagram to the topmost base class in this class hierarchy to identify several is-a relationships. For example, 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 also are TwoDimensionalShapes, and cylinders also are ThreeDimensionalShapes.

Figure 10.3. Inheritance hierarchy for Shapes.


Notes on Inheritance Relationships

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 inherit 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). Later in this chapter and in Chapter 11, we consider many examples that use the is-a relationship.

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, the 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 implementation appropriate for the derived class, as we will see often in the chapter's code examples.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net