4.1.12

When not to inherit !

I dedicate this blog to mister matthew fenwick.

So -- here it is, to clarify - good inheritance vs. bad inheritance :

An example of GOOD Inheritance :

I have an object, it needs to implement services A,B,C,and D. A depends on B and C. C depends on D.

However, A , B, and C are all generic, whereas D is highly specified and varies from class to class.

Make a class that implements A , B and C, defining an abstract D function --- and inherit from it.


An example of BAD inheritance :

I have a object, which needs access to several variables "phoneNumber,ssid,name". Later, I realize that I have another object which stores the same data. Inherit this object from the original object.

This is BAD BAD BAD ! You are inheriting data that is not particularly complex, and can easily be decentralized. You are coupling the implementation of two classes which have no behavioural similarity. BAD BAD BAD ! This is why matthew fenwick hates inheritance.

No comments:

Post a Comment