These sorts of considerations lead software developers to create a class hierarchy. The programmer defines
the more general state variables and behavior methods in higher-level classes. Then, when writing the subordinant
classes, the programmer uses the inherited state and behavior when it fits, and adds state variables, methods,
and overriding methods to subordinant classes in order to implement differences between the superior and
subordinant classes.
The beauty of this technique is that classes which are written and tested do not change. Existing software
functionality can be reused. When the programmer requires new features in a class, the programmer can inherit
from an existing, tested class, and write new software simply to meet the new requirements.
If one decides to implement the accelerate() method differently for different Automobiles, one
would write the parent class Automobile, and then have subordinant classes inherit from Automobile.
In our example, we might design several classes to inherit from Automobile, a class called EconomyCar,
a class called FamilyCar, and a class called SportsCar. Each of these subordinant classes would inherit
the accelerate() method from the Automobile class, and could override the inherited accelerate()
method to change the acceleration behavior of instances of the subordinant class.
Pages:
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219