There are, BTW, good reasons that Java doesn't allow multiple inheritance. For example, consider
If you call whatDoesThisDo()
on a Boojum object, which version of x does it increment? Which version of y does it decrement? Which version of doSomething() does it call? It's not clear what the program is supposed
to do, much less how to make the compiler and JVM do that.
In C++, there are obscure rules to control which of these things happens, and you can change which one happens by using the keyword "virtual" (yet another of the half-dozen meanings of that keyword in C++). The designers of Java decided to avoid all those problems by just not allowing multiple inheritance. It turns out that to avoid all these vexing issues, it suffices to make sure no class inherits from two different parents that have instance variables, and no class inherits from two different parents that have method implementations; these are exactly the constraints imposed on the Java "interface" construct, so we can inherit from multiple interfaces but only one class.
That said, there are
situations in which multiple inheritance would be nice. For example, I like to introduce linked lists in the classroom via polymorphism, rather than with an "if (current == null)" statement. (For details, see my paper on the subject
.) When I move on to doubly-linked lists, I may write
It would save some trouble, and avoid some duplicate code, if HasSuccessor and HasPredecessor could have instance variables and default implementations of certain methods. But they can't, in Java. Arguably a small sacrifice for avoiding all the headaches of multiple inheritance.
[ March 29, 2005: Message edited by: Stephen Bloch ]