Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

like image 857
Starx Avatar asked May 16 '10 12:05

Starx


People also ask

Why is multiple inheritance not supported?

The reason behind this is to prevent ambiguity. Consider a case where class B extends class A and Class C and both class A and C have the same method display(). Now java compiler cannot decide, which display method it should inherit. To prevent such situation, multiple inheritances is not allowed in java.

Which programming language does not support multiple inheritance?

4. Which programming language restricts the use of multiple inheritance? Explanation: Java doesn't allow use of multiple inheritance with classes.

Do all programming languages support multiple inheritance?

Languages that support multiple inheritance include: C++, Common Lisp (via Common Lisp Object System (CLOS)), EuLisp (via The EuLisp Object System TELOS), Curl, Dylan, Eiffel, Logtalk, Object REXX, Scala (via use of mixin classes), OCaml, Perl, POP-11, Python, R, Raku, and Tcl (built-in from 8.6 or via Incremental Tcl ...

Why multiple inheritance is not supported in Python?

Yes, Python supports multiple inheritance. Like C++, a class can be derived from more than one base classes in Python.


2 Answers

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.

like image 74
Oddthinking Avatar answered Oct 13 '22 10:10

Oddthinking


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).

like image 34
danben Avatar answered Oct 13 '22 12:10

danben