Java Inheritance
by Tom Temple
21 September 2005
I was lucky enough to learn C++ first. So when I learned Java it was like prayers being answered. Most people these days do it the other way. Jon, could you put the great quote from [My copy of] “C++ for Java programmers” if you still have it.
I am taking an upper-level undergrad class in automation to prep for the quals. They conveniently let me get credit for it if I do a few suplemental assignments. Right now they are strolling CS25 presumably on their way to bigger and better things. I guess we can afford to stroll since the term is so much longer here. The strange thing being back in an undergrad class is how much hand holding there is.
The grad class would say, “Provide test code using Junit” (What the hell is Junit). The undergrad class is like “There are these testing tools called Junit… We’ve installed Junit on the lab computers, here’s a link to their intro page, and the TAs will have a tutorial on wednesday evening.”
Naturally, I am getting the “Don’t waste my time telling me things I can find out faster with Google.” attitude. Worse still, we’re blowing tons of lecture-time talking about how to implement things in Java. Litterally CS5 material; for the undergrads it was a prerequisite; and I don’t think it would be a lot to ask that engineering grad students know how to write in Java.
Finally the point: The prof has spent no less than 10 min (cumulatively) bemoaning the lack of multiple inheritance in Java. An understandable complaint, but he was being particularly incisive—chalking the decision up to laziness and designing themselves into a corner.
I almost called him out but I wasn’t sure I had my facts straight. I’m pretty sure that the reason was a good one based on how little time I have spent worrying about dynamic casts in Java. Michael, what exactly does making inheritance a tree buy for you? Can an inheritance DAG do the same thing?

Sep 21, 01:10 PM
Only a tiresome old C++ partisan could possibly think multiple inheritance is a good idea; either that, or somebody who has never had to maintain a complex system designed with multiple inheritance. That’s hardly the worst problem with C++, though. Bjarne Stroustrup ought to have his ACM card revoked for inflicting that godawful monster on the world. I tell you, him and John Osterhout will be the first against the wall when the revolution comes…but I digress.
Java does allow a limited form of multiple inheritance, via interfaces. This allows you to get most of the benefits, while avoiding most of the drawbacks of unrestricted multiple inheritance. A couple of the more obvious problems are what to do with the types of identically-named member variables which you inherit with different types along more than one path—this complicates the semantics of method dispatch. Also, when you have multiple parents, your method resolution order is much more complicated, and programmers do not seem to deal well with this in practise. And, if that’s not enough, it makes the fragile base class problem even harder to avoid.
Interfaces don’t let you pass along member variables, only method signatures, and an interface can’t derive from other interfaces, so you can detect and avoid most of the idiocy with method resolution statically.