
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
The Theory of Inheritance
|
203
Why Inheritance?
Superficially, the obvious benefit of inheritance is code reuse. Inheritance lets us sep-
arate a core feature set from customized versions of that feature set. Code for the
core is stored in a superclass while code for the customizations is kept neatly in a
subclass. Furthermore, more than one subclass can extend the superclass, allowing
multiple customized versions of a particular feature set to exist simultaneously. If the
implementation of a feature in the superclass changes, all subclasses automatically
inherit the change.
But inheritance also lets us express the architecture of an application in hierarchical
terms that mirror the real world and the human psyche. For example, in the real
world, we consider plants different from animals, but we categorize both as living
things. We consider cars different from planes, but we see both as vehicles. Corre-
spondingly, in a human resources application, we might have an Employee super-
class with Manager, CEO, and Worker subclasses. Or, in a banking application, we
might create a BankAccount superclass with CheckingAccount and SavingsAccount
subclasses. These are canonical examples of one variety of inheritance sometimes
called subtype inheritance, in which the application’s class hierarchy ...