Understanding Inheritance
As already explained, one of the biggest advantages in adopting OOP is about code reuse, a concept
nicely summarized in the DRY (Don ??™ t Repeat Yourself!) acronym. A way to achieve code reuse is quite
simple in theory: When there are two or more classes that share common pieces of code, group them in
functions and move those functions to a base super - class.
Inheritance from another point of view can be seen as a hierarchy tree with a generic system at its root,
the concept of vehicle for example, and a more specialized system, like car or boat , on each branch; the
hierarchy can have an unlimited number of levels with several degrees of specialization. Every new level
in the hierarchy may add new functions and new variables: the class Vehicle should have the method
accelerate() which is inherited by all its descendants and the child class Car should have a variable
numberOfWheels that is not present in its sibling Boat .
After all, extending a class means creating a more specialized and feature - rich version of an existing class.
Pages:
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249