Questions tagged [multiple-inheritance]

A feature of some object-oriented computer programming languages in which a class can inherit behaviors and features from more than one superclass or base class.

Languages that support multiple inheritance include:

  • C++
  • Common Lisp (via CLOS)
  • Curl
  • Dylan
  • Eiffel
  • EuLisp (via The EuLisp Object System TELOS)
  • Logtalk
  • Object REXX
  • OCaml
  • Perl
  • Perl 6
  • Python
  • Scala (via the use of mixin classes)
  • Tcl (via Incremental Tcl)

Other object-oriented languages, such as Java and Ruby implement single inheritance, although protocols or "interfaces" provide some of the functionality of true multiple inheritance.

2465 questions
1084
votes
16 answers

What is a mixin, and why are they useful?

In "Programming Python", Mark Lutz mentions "mixins". I'm from a C/C++/C# background and I have not heard the term before. What is a mixin? Reading between the lines of this example (which I've linked to because it's quite long), I'm presuming it's…
TarkaDaal
  • 16,152
  • 7
  • 32
  • 50
1021
votes
16 answers

How does Python's super() work with multiple inheritance?

I'm pretty much new in Python object oriented programming and I have trouble understanding the super() function (new style classes) especially when it comes to multiple inheritance. For example if you have something like: class First(object): …
Callisto
  • 10,221
  • 3
  • 13
  • 4
626
votes
11 answers

What does 'super' do in Python? - difference between super().__init__() and explicit superclass __init__()

What's the difference between: class Child(SomeBaseClass): def __init__(self): super(Child, self).__init__() and: class Child(SomeBaseClass): def __init__(self): SomeBaseClass.__init__(self) I've seen super being used quite…
user25785
  • 6,263
  • 3
  • 16
  • 5
225
votes
10 answers

Calling parent class __init__ with multiple inheritance, what's the right way?

Say I have a multiple inheritance scenario: class A(object): # code for A here class B(object): # code for B here class C(A, B): def __init__(self): # What's the right code to write here to ensure # A.__init__ and…
Adam Parkin
  • 14,883
  • 13
  • 57
  • 81
223
votes
14 answers

Multiple Inheritance in C#

Since multiple inheritance is bad (it makes the source more complicated) C# does not provide such a pattern directly. But sometimes it would be helpful to have this ability. For instance I'm able to implement the missing multiple inheritance pattern…
Martin
  • 9,968
  • 12
  • 56
  • 67
172
votes
15 answers

Why should I avoid multiple inheritance in C++?

Is it a good concept to use multiple inheritance or can I do other things instead?
Hai
  • 4,454
  • 8
  • 27
  • 26
172
votes
9 answers

How to make a Java class that implements one interface with two generic types?

I have a generic interface public interface Consumer { public void consume(E e); } I have a class that consumes two types of objects, so I would like to do something like: public class TwoTypesConsumer implements Consumer,…
daphshez
  • 8,294
  • 11
  • 40
  • 57
169
votes
17 answers

Java Multiple Inheritance

In an attempt to fully understand how to solve Java's multiple inheritance problems I have a classic question that I need clarified. Lets say I have class Animal this has sub classes Bird and Horse and I need to make a class Pegasus that extends…
155
votes
2 answers

Mixins vs. Traits

What is the difference between Mixins and Traits? According to Wikipedia, Ruby Modules are sort of like traits. How so?
KaptajnKold
  • 9,810
  • 8
  • 39
  • 52
135
votes
17 answers

Multiple inheritance/prototypes in JavaScript

I've come to a point where I need to have some sort of rudimentary multiple inheritance happening in JavaScript. (I'm not here to discuss whether this is a good idea or not, so please kindly keep those comments to yourself.) I just want to know if…
devios1
  • 33,997
  • 43
  • 149
  • 241
135
votes
7 answers

Can an interface extend multiple interfaces in Java?

Can an interface extend multiple interfaces in Java? This code appears valid in my IDE and it does compile: interface Foo extends Runnable, Set, Comparator { } but I had heard that multiple inheritance was not allowed in Java. Why does…
Prateek
  • 6,239
  • 2
  • 22
  • 37
123
votes
11 answers

What is the exact problem with multiple inheritance?

I can see people asking all the time whether multiple inheritance should be included into the next version of C# or Java. C++ folks, who are fortunate enough to have this ability, say that this is like giving someone a rope to eventually hang…
Vlad Gudim
  • 22,473
  • 16
  • 66
  • 91
117
votes
17 answers

Why is Multiple Inheritance not allowed in Java or C#?

I know that multiple inheritance is not allowed in Java and C#. Many books just say, multiple inheritance is not allowed. But it can be implemented by using interfaces. Nothing is discussed about why it is not allowed. Can anybody tell me precisely…
Abdulsattar Mohammed
  • 8,916
  • 12
  • 49
  • 66
100
votes
5 answers

How does virtual inheritance solve the "diamond" (multiple inheritance) ambiguity?

class A { public: void eat(){ cout<<"A";} }; class B: virtual public A { public: void eat(){ cout<<"B";} }; class C: virtual public A { public: void eat(){ cout<<"C";} }; class D: public B,C { public: void eat(){…
99
votes
6 answers

Multiple inheritance for an anonymous class

How can an anonymous class implement two (or more) interfaces? Alternatively, how can it both extend a class and implement an interface? For example, I want to create an object of anonymous class that extends two interfaces: // Java 10 "var" is…
user707549
1
2 3
99 100