Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Java omits multiple inheritance on the grounds that it obviates the design goal of keeping the language simple.

I wonder if Java (with its eco-system) is really "simple". Python is not complex and has multiple inheritance. So without being too subjective, my question is...

What are the typical problem patterns that benefit from a code designed to make heavy use of multiple inheritance

share|improve this question
Java omits an awful lot of perfectly good things for very little reason. I wouldn't expect a decent justification for MI. –  DeadMG Aug 14 '11 at 12:50
Python's multiple inheritance is definitely here-be-dragons territory. The fact that it uses depth-first, left-to-right name resolution has significant issues for both maintainability and comprehension. While it can be useful in shallow class hierarchies, it can be a incredibly counter-intuitive in deep ones. –  Mark Booth Aug 15 '11 at 13:00

6 Answers 6

up vote 6 down vote accepted

Pros :

  1. It sometimes allow more obvious modeling of a problem than other ways to model it.
  2. If the different parrents have orthogonal purpose, it can allow some kind of compositing

Cons :

  1. If the different parents don't have orthogonal purpose, it makes the type difficult to understand.
  2. It's not easy to understand how it is implemented in a language (any language).

In C++ a good example of multiple inheritance used to composite orthogonal features is when you use CRTP to, for example, setup a component system for a game.

I've started to write an example but I think a real world example is more worth looking at. Some code of Ogre3D uses multiple inheritance in a nice and very intuitive way. For example, the Mesh class inherit from both Resources and AnimationContainer. Resources expose the interface common to all resources and AnimationContainer expose the interface specific for manipulating a set of animations. They are not related, so it's easy to think about a Mesh as being a resource that in addition can conain a set of animations. Feels natural isn't it?

You can look at other examples in this library, like the way memory allocation is managed in a fined grain way by making classes inherit from variants of a CRTP class overloading new and delete.

As said, the main problems with multiple inheritance rises from mixing related concepts. It makes the language have to set complex implementations (see the way C++ allows to play with the diamond problem...) and the user not being sure what's happening in that implementation. For example, read this article explaining how it is implemented in C++.

Removing it from the language helps avoiding people who don't know how the language is inforced to make things bad. But it forces to think in a way that, sometimes, don't feel natural, even if it's edge cases, it happen more often that you might think.

share|improve this answer
i would really appreciate if you adorned your answer with an example problem -- that will make terms like "orthogonal purpose" clearer -- but thanks –  treecoder Aug 14 '11 at 12:14
Ok let me try to add something. –  Klaim Aug 14 '11 at 14:19
Ogre3D is not any place I would look to for design inspiration- have you seen their Singleton infection? –  DeadMG Aug 14 '11 at 17:13
First, heir singleton isn't really a singleton, the construction and destruction is explicit. Next, Ogre is a layer over a hardware system (or the graphic driver if you prefer). That mean there should be only one unique representation for system interfaces (like Root or others). They can remove some singleton but that's not the point here. I've voluntarly avoided to point this to avoid having a troll discussion, so please, look at the examples I've pointed. Their use of Singleton might not be perfect but it's clearly useful in practice (but only for their kind of system not everything). –  Klaim Aug 14 '11 at 17:19

There is a concept called mixins that is used heavily in more dynamic languages. Multiple inheritance is one way in which mixins can be supported by a language. Mixins are generally used as a way for a class to accumulate different pieces of functionality. Without multiple inheritance, you have to use aggregation / delegation to get mixin type behavior with a class, which is a bit more syntax heavy.

share|improve this answer
+1 this is actually a good reason to have multiple inheritence. Mixins carry additional connotations ("this class shouldn't be used as a stand-alone") –  ashes999 Nov 9 '13 at 17:49

I think the choice is mainly based on issues due to the diamond problem.

Moreover, it is often possible to circumvent the use of multiple inheritance by delegation or other means.

I'm not sure of the meaning of your last question. But if it is "in which cases is multiple inheritance useful?", then in all cases where you would like to have an object A having functionalities of objects B and C, basically.

share|improve this answer

I won't delve much in here but you can surely understand the multiple inheritance in python via the following link http://docs.python.org/release/1.5.1p1/tut/multiple.html :

The only rule necessary to explain the semantics is the resolution rule used for class attribute references. This is depth-first, left-to-right. Thus, if an attribute is not found in DerivedClassName, it is searched in Base1, then (recursively) in the base classes of Base1, and only if it is not found there, it is searched in Base2, and so on.


It is clear that indiscriminate use of multiple inheritance is a maintenance nightmare, given the reliance in Python on conventions to avoid accidental name conflicts. A well-known problem with multiple inheritance is a class derived from two classes that happen to have a common base class. While it is easy enough to figure out what happens in this case (the instance will have a single copy of ``instance variables'' or data attributes used by the common base class), it is not clear that these semantics are in any way useful.

This is just a small paragraph but big enough to clear the doubts I guess.

share|improve this answer

One place where multiple inheritance would be useful is a situation where a class implements several interfaces, but you would like to have some default functionality built in to each interface. This is useful if most of the classes that implement some interface want to do something the same way, but occasionally you need to do something different. You can have each class with the same implementation, but it makes more sense to push it up into one location.

share|improve this answer
Would that require generalized multiple inheritance, or simply a means by which an interface can specify default behaviors for unimplemented methods? If interfaces could only specify default implementations for the methods they themselves implement (as opposed to those they inherit from other interfaces) such a feature would completely avoid the double-diamond issues which make multiple inheritance difficult. –  supercat Dec 24 '13 at 18:13

First: multiple copies of base class (a C++ issue) & tight coupling between base and derived classes.

Second: multiple inheritance from abstract interfaces

share|improve this answer
are you suggesting it is not useful in any context? And that everything can be designed/coded conveniently without it? Also please elaborate on the second point. –  treecoder Aug 14 '11 at 12:11

protected by gnat Jul 10 at 14:35

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.