|
|
|
|
|
|
|
Figure 16-3
Inheritance Hierarchy |
|
|
|
|
|
|
|
|
child (and all of its descendants). A wheeled vehicle inherits properties common to all vehicles (it holds one or more people and carries them from place to place) but has an additional property that makes it more specialized (it has wheels). A car inherits properties common to all wheeled vehicles but also has additional, more specialized properties (four wheels, an engine, a body, and so forth). |
|
|
|
|
|
|
|
|
The inheritance relationship can be viewed as an is-a relationship. Every two-door car is a car, every car is a wheeled vehicle, and every wheeled vehicle is a vehicle. |
|
|
|
|
|
|
|
|
OOP languages provide a way of creating inheritance relationships among classes. In these languages, inheritance is the mechanism by which one class acquires the properties of another class. You can take an existing class A (called the base class or superclass) and create from it a new class B (called the derived class or subclass). The derived class B inherits all the properties of its base class A. In particular, the data and operations defined for A are now also defined for B. (Notice the is-a relationshipevery B is also an A.) The idea, next, is to specialize class B, usually by adding specific properties to those already inherited from A. Let's look at an example in C++. |
|
|
|
 |
|
 |
|
|
Inheritance A mechanism by which one class acquires the propertiesthe data and operationsof another class. |
|
|
|
 |
|
 |
|
|
Base Class (Superclass) The class being inherited from. |
|
|
|
 |
|
 |
|
|
Derived Class (Subclass) The class that inherits. |
|
|
|
|
|