Implementing Inheritance 373
12
The Animal Kingdom ....................................................................................
To facilitate the discussion of derivation and inheritance, this chapter focuses on the rela-
tionships among a number of classes representing animals. You can imagine that you
have been asked to design a children’s game—a simulation of a farm.
In time, you will develop a whole set of farm animals, including horses, cows, dogs, cats,
sheep, and so forth. You will create methods for these classes so that they can act in the
ways the child might expect, but for now you’ll stub-out each method with a simple print
statement.
Stubbing-out a function means you’ll write only enough to show that the function was
called, leaving the details for later when you have more time. Please feel free to
extend the minimal code provided in today’s lesson to enable the animals to act more
realistically.
You should find that the examples using animals are easy to follow. You also find it easy
to apply the concepts to other areas. For example, if you were building an ATM bank
machine program, then you might have a checking account, which is a type of bank
account, which is a type of account. This parallels the idea of a dog being an mammal,
which in turn is an animal.
FIGURE12.1
Hierarchy of animals.
Animal
Mammal Reptile
Horse Dog
Retrievers Spaniels
Labradors Goldens
Working Sporting Terriers