e Abuse of Inheritance

Abuse of Inheritance

With inheritance there is a risk of creating programs which are difficult to maintain and extend.

If new classes are built by inheriting operations and attributes in a random way from existing classes ("that method looks nice -- I think I'll let my new class inherit from the class where it is defined"), without considering whether this is actually a good model of reality, we may end up with a program where the classes are entangled like a bowl of spaghetti.

"A good model of reality" means in this case that class B "is-a-kind-of" class A -- that there is a generalization-specialization relationship between the classes.

The spaghetti-effect may be multiplied with the abuse of multiple inheritance.

An alternative to the above, when we want to reuse an operation in another class, is to request this operation from an object of the class where it is defined. This requires doing some design work, in order to plan how the objects in the system should be inter-connected.

Exercise on Inheritance

previous | next | start | contents | dictionary | help | evaluation

© Ericsson Telecom AB, 1995, Stockholm, Sweden