December 10, 2002
Java multiple inheritance

Multiple inheritance - good or bad? The only good answer to this is 'it depends'.

There are a number of programming language facilities which can very easily be abused, but which when not abused can be very powerful. Multiple inheritance is one of these. Another which springs to mind is operator overloading.

The designers of Java decided that the danger of including these was greater than the benefit.

The designer of Python decided to trust the programmer to know what he or she is doing. Is this a mistake? I don't think so, but then I've never had to work on a large Python project alongside incompetent developers. Of course, incompetent developers can screw up in any language, so I can't see Python making anything worse.

Anyway, I digress. One of the main good uses of multiple inheritance is 'mixins'. To my understanding, these are classes created specifically to be multiply inherited, and which provide specific behaviours which might be useful to any class. A good example of this in Python is found in ZOBD, an object database. To allow an existing class to have its instances stored in the database, all you have to do is to inherit from the ZODB.Persistent mixin class in addition to whatever your class is already inheriting from.

Java as it stands won't allow anything like mixins, but there is a Java extension, Jam, which does.

A recent developerworks article, Diagnosing Java code: Killer combo -- Mixins, Jam, and unit testing, shows how mixins could be used to simplify unit testing in some circumstances. I'm not sure that this is a killer-app for Jam - unless I'm missing something, AspectJ might be a better approach here. Interesting, nevertheless.

Posted to Java by Simon Brunning at December 10, 2002 04:57 PM
Post a comment

Email Address:



Remember info?