![]() |
|||||||||
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:
Learning how to exploit the power of inheritance is the hallmark of a successful object-oriented programmer.
|
|||||||||
|
|||||||||
ÿ