The Fallacies of Inheritance

Share the article!

I am absolutely surprised at the level of objections to Alan Holub’s “Why extends is evil” article. What troubles me is the extent of ignorance prevailing on the subject of inheritance.

First, many people seem to not recognize implementation inheritance. Thanks to java, they do recognize interface inheritance as being distinct from class inheritance. However, they don’t recognize that with class inheritance there are two kinds in play. That is implementation and “interface” inheritance. Cargill in his 1993 book on C++ programming style addressed this same very issue. He recommends that implementation inheritance is best done via delegation.

Second, inheritance is not essential to Object Oriented Programming. The essential features of OOP is Identity, Encapsulation and Polymorphism. Inheritance is only a means to achieve polymorphism in a statically typed language. Alot of people seem to have been educated with the notion that inheritance is what defines OOP, this is a fallacy. An excellent exploration of this can be found in Charles Miller’s blog on “Inheritance Taxonomy“.

Finally, people have the mistaken notion that the Object Model of Java is adequate for modeling. This is possibly the biggest fallicy of them all. Java’s object model has constraints that make it difficult to naturally model domains.

For starters there’s only one kind of Class object and objects are restricted to being instances of this class object (i.e. class object dichotomy). Furthermore, you cannot dynamically change the class of an object (i.e. static inheritance). Classes assume a total classification that’s not realistic. Fortunately, interfaces help in break this rigidity by allowing partial classification, but the same constraints for classes mentioned above still apply.

The sound approach to modeling using Java is to develop an Adaptive Object Model as defined by Ralph Johnson (GOF fame). The Java object model should only come into play in the implementation, but it simply shouldn’t influence how you model a domain.

It’s been a decade since Cargill wrote about “unnecessary inheritance”. What’s dissapointing is that so many people have either ignored his wisdom or have simply been poorly educated.

In summary the fallacies are that there’s only one kind of inheritance, inheritance is essential to OOP and static inheritance is all that’s ever needed.

Share the article!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>