Monday, November 01, 2004

Inheritance

Delegate Now

Ouch. Sometimes it scares me how easily programmers can confuse common knowledge. This is one of those times.

The pattern goes like this. Someone takes a readily understood and accepted concept, removes it from its original context, confuses it thoroughly, gives different meaning to it, and then proceeded to extoll the virtues of the new found wisdom as though it were something other than misplaced enthusiasm.

Every language deals with inheritance in a different way. To examine the two mentioned above; Java uses single inheritance, public/private/protected access, abstract classes and interfaces as language constructs, but lacks a mechanism for composition via inheritance. C++ provides multiple inheritance, public/private/protected access, abstract classes and interfaces (via pure virtual methods), but also provides for composition via inheritance and even virtual inheritance. C++ can also alter the access of a class at composition via inheritance using public/private/protected.

In some cases these features are similar and in others they are vastly different. The way C++ uses inheritance is not the same as the way Java uses inheritance. In C++ it actually makes a lot more sense to do composition via inheritance, so inheriting implementation and behaviour is far more common.

In fact, that is the essential point. The issue at stake is not wether inheritance is good or bad, but wether you want to create a heirarchy of classes which share a common behaviour, or simply a common interface.

The reason why most API's implement a Stack as a subclass of List is becuase it makes perfect sense to share the behaviour. To misunderstand this is to misunderstand object oriented programming.

Clarifying what you want and understanding the concepts of type, composition, behaviour, interitance, polymorphism, and deligation are key to object oriented programming. These constructs have not been arrived at without reason or purpose.

Stating that implementation inheritance is bad and should be avioded altogether simly shows that you have not understood the issues at hand. There is no one size fits all solution.