6.1 Introduction to Inheritance


Generalization captures the common or shared aspects of abstractions. It is not unusual to find such generalizations in everyday experience - for example, the statement "all computer workstations have a monitor" expresses a generalization about computer workstations, and identifies a common attribute among the various abstractions for a computer workstation. Similarly, the statement "all telephones can make connections through the telephone switching system" expresses a similarity in behavior: all abstractions of telephones will possess a behavior that allows them to make connections.

When a group of abstractions in an application domain share similar features, the classes representing these abstractions may also be similar. This is natural because a class is intended to reflect the attributes and behavior of a class. Thus, similarities in the abstractions will result in similarities among the classes representing them. However, nothing that has been seen so far allows a group of classes to express any similarites that they might share.

Inheritance expresses sharing among classes. Classes may share a common implementation (code and/or data), a common interface, or both. When inheritance is used to create interface sharing, each class sharing the common interface must implement the methods described in the shared interface, although these classes may have other methods in addition to those in the shared interface. The classes sharing a common interface can, and usually do, implement differently the methods in the shared interface. When inheritance is used to create implementation sharing, each of the sharing classes builds on the shared implementation to define its own operations.

Inheritance extends the class concept to distinguish between a base class which defines the shared interface and implementation, and a derived class that inherits the shared elements from the base class. Equivalent terminology used in other object-oriented languages is superclass and subclass to refer to base and derived classes, respectively.

Inheritance is the most critical aspect of object oriented programming because it has implications in several important dimensions. Three of these dimensions are:

  • programming: expressing the similarities among a related set of classes in a single base class makes unnecessary the repetition of code in each of the derived classes. Not only does this create an immediate savings for the classes at hand, but it also establishes a foundation on which future derived classes can be built more efficiently.

  • design: in the largest sense, software design is a form of knowledge engineering, because it attempts to represent in software a model of the real world. A good designer does more than organize software structures; he or she tries to reflect in the software the existence and organization of abstractions in the problem domain. Thus, if there are similarities among the abstractions, there should be a corresponding way to represent these similarities in the software. The more direct and explicit the software can be in representing these similarities, the better a model it will be of the application domain.

  • software engineering: software has a long lifetime and undergoes many changes and extensions during its existence. Therefore, important qualities of software are the degrees to which it is flexible - can be modified to add new features, and extensible - can accomodate the creation of new elements not originally anticipated.

Learning how to exploit the power of inheritance is the hallmark of a successful object-oriented programmer.

Inheritance Hierarchy

Sharing Implementations

Inheriting Operations and Data

 

Revising an Inheritance Hierarchy

Replacing Inherited Methods

Extending Inherited Methods

Hiding Inherited Methods

 

Polymorphism

Type Casting

Interface Sharing

Refactoring of Base Classes

Multiple Inheritance

 




©1998 Prentice-Hall, Inc.
A Simon & Schuster Company
Upper Saddle River, New Jersey 07458

Legal Statement

 

 

 

ÿ