Multiple Inheritance in Java

For those who read this title and were strangely intrigued and/or confused: gotcha! For those who are genuinely inquisitive on how to accomplish multiple inheritance in Java, I won’t string you along any further: multiple inheritance in Java does not exist. Although some may argue that this is a shortcoming of Java, most of the implementations of multiple inheritance can be accomplished by utilizing interfaces. However, Dr. Joseph Bergin discusses a situation where multiple inheritance is required and how to accomplish it in Java.

Dr. Bergin dubs this special kind of multiple inheritance as "mixin inheritance". A mixin situation is where you might have a particular function or connection that is required in a variety of different classes. Since these other classes might inherit from other relevant superclasses, there is a need to inherit from both the actual desired superclass and this common class containing the desired function.

In this scenario, there are two possible implementations. In the first scenario, we are creating one of the classes, and thus can create one of them to be multiple inheritance friendly. The other scenario is where our mixin class and superclass are already predefined. When our classes are already defined, we then create a wrapper class that will allow us to implement multiple inheritance with our predefined classes.

I won’t bother repeating all of Dr. Bergin’s code here, so I’ll point you over to his article again.

Dr. Bergin’s article is an example of the flexibility that programming languages provide. Although this example is specifically for Java, the spirit of his implementation is applicable to all programming languages. Although intentionally left out by the framers of Java, multiple inheritance is fairly common. Instead of cursing Java, Bergin implemented a working solution to his problem.

Don’t keep your clever workarounds and tricks for Java to yourself! Post them in the comments here.

Published
Categorized as Java

By Michael Marr

Michael Marr is a staff writer for WebProNews

Leave a comment