Saturday, December 04, 2004

Multiple Inheritance

It's been quite a while since I have had anything to say about Java but an article on JavaLobby made me want to respond. The gist of the article is that multiple inheritance is wonderful and would solve many problems for developers and Java should allow it. Let me just sum up my feelings by saying that this is an incredibly bad idea. The lack of multiple inheritance in Java is a good thing, not a defect.

There are two rules that sum up my thinking on this. One comes from Joshua Bloch and the other comes from the gang of four:

1) Favor composition over inheritance
2) Inherit interfaces and not implementations

Inheritance is abused in OO programming and allowing multiple inheritance is inviting more abuse. We should program to interfaces and use those to support multiple inheritance. We should combine separate interfaces in a single class in order to provide the appearance of multiple inheritance. On the good side, the author of the original article did write a post script in which he admitted that he could get around his problem by using interfaces. And that is exactly the right way to do it!

1 comment:

Elblog said...

I'm reminded of my favorite "computing moment" of all time. I was in an annual user's conference with the owner and developer of an application. After about a 5 minute rant from one user about a particular aspect of the software, including an impasssioned speech about how it should work, the owner smiled and said, "That's not a bug, that's a FEATURE." He then when on to explain why, and it was clear that he'd already "been there, done that" in terms of what the user was saying.
The phrase has been with me ever since.