13

Why is multiple inheritance not supported in most of programming language?

I could really use this feature to develop different layout of application?

danben
  • 80,905
  • 18
  • 123
  • 145
Starx
  • 77,474
  • 47
  • 185
  • 261
  • Some reasons: http://stackoverflow.com/questions/191691/should-c-include-multiple-inheritance, http://stackoverflow.com/questions/406081/why-should-i-avoid-multiple-inheritance-in-c, http://stackoverflow.com/questions/225929/what-is-the-exact-problem-with-multiple-inheritance – kennytm May 16 '10 at 12:44
  • Many languages do support it through implementing multiple interfaces, as does PHP. Could you explain what it is you're trying to do? – Bart Kiers May 16 '10 at 12:45
  • I am combining various class and build one major class through which I will have access to all methods in all of the sub classes. My code library is very big, so I was trying to seperate it. – Starx May 16 '10 at 13:14
  • 1
    hehe--I guess the answer is obvious then, the reason most languages don't support it is because programmers choose to do stuff like that^^^. Look up single responsibility principle. My suggestion to you would be to remove ALL your inheritance as a practice and just use interfaces. After doing this for a while, the few cases where you actually should use inheritance should become obvious. Also remember each class should be tiny and only do one very simple thing. – Bill K Apr 13 '11 at 10:03
  • @Bill K, I have come a long way since I asked this question, now I am pretty sure, of what I should do. Thanks for you tips. – Starx Apr 13 '11 at 10:05
  • @Starx it's a great question though--glad it's here. Many programmers (all?) seem to abuse inheritance when they first figure it out. You may want to edit your question to include what you've learned. – Bill K Apr 13 '11 at 20:22
  • http://stackoverflow.com/questions/225929/what-is-the-exact-problem-with-multiple-inheritance/226056#226056 – jaybny May 28 '13 at 03:36

3 Answers3

14

Multiple inheritance is useful in many situations as a developer, but it greatly increases the complexity of the language, which makes life harder for both the compiler developers and the programmers.

  • One problem occurs when two parent classes have data members or methods of the same name. It is difficult to resolve which is being referenced by the sub-class.

  • Another occurs when two parent classes inherit from the same base class, forming a "diamond" pattern in the inheritance hierarchy.

  • The order that the initialisation/elaboration of the parent classes needs to be specified - this can sometimes lead to behaviour changing when the order of the inheritance changes - something may catch developers by surprise.

  • Some languages support a reference to 'super', or equivalent, which refers to an attribute of the base class for this object. That becomes difficult to support in a language with multiple inheritance.

  • Some languages attempt to provide an automatic Object-Relational Model, so the objects can be made persistent with a regular RDMS. This mapping is difficult at the best of times (it has been described as the "Vietnam War" of software development), but it is much more difficult if multiple inheritance is supported.

Oddthinking
  • 24,359
  • 19
  • 83
  • 121
4

One reason not to support it is ambiguity of method resolution.

http://en.wikipedia.org/wiki/Diamond_problem

However, I'm not sure what you mean by "most" programming languages. Many that are in use today support it directly (C++, Python, Perl, OCaml) or have a mechanism for similar functionality (Ruby and Scala come to mind).

danben
  • 80,905
  • 18
  • 123
  • 145
-7

The real reason why multiple inheritance is not supported across many langauges, is just the laziness of language developers. To cover up this embarrassing failure, all sorts of excuses are made, "it makes life difficult for the developer" bla bla, but for anyone who has actually used a language that implements it well, multiple inheritance becomes natural and easy after about 1 month. No big deal.

The only problem with it is after you have realized how useful and easy it is, you tend to become allergic to languages that don't support it and this may constrain your career prospects.

So my advice would be to stay well away from it.

Finnian Reilly
  • 177
  • 1
  • 3
  • just out of curiosity: what is your preferred language and in which language is the multiple inheritance solved best? – sodik Jun 24 '15 at 11:39