
Principles of Object-Oriented Programming | 27
OOP
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
Inheritance
Inheritance makes it possible for OOP code to build classes that extend or restrict
features in other existing classes, without the need to fully rewrite the original
class. For instance, a class of Pet may have generic data fields such as Name, Age,
and Color. This single class could be extended into other, more specific classes
through inheritance. A class named Dog that is derived from Pet would automati-
cally include the Name, Age, and Color members, but it may add additional
canine-specific members such as Breed and a ShedsHair flag. In this situation, the
Dog class inherits from the Pet class.
Inheritance used in this manner certainly reduces duplication of code, since the
derived class does not have to rewrite the code for the existing base class’s
members. But inheritance also makes interactions between these objects easier,
since an object of type Dog is also a true object of type Pet. Objects of a derived
class are also objects of the base class, and they can be used in code as if they were
actually members of the base class. (The reverse is not true; objects of type Pet are
not necessarily objects of type Dog).
Some languages allow a class to inherit from multiple base classes at the same
time. Visual Basic does ...