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.

2710 questions
1319
votes
18 answers

What is a mixin and why is it useful?

In Programming Python, Mark Lutz mentions the term mixin. I am 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 have linked to because it is quite long), I am…
TarkaDaal
  • 18,798
  • 7
  • 34
  • 51
1306
votes
18 answers

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

How does super() work with multiple inheritance? For example, given: class First(object): def __init__(self): print "first" class Second(object): def __init__(self): print "second" class Third(First, Second): def…
Callisto
  • 13,071
  • 3
  • 16
  • 4
743
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…
user25785
  • 7,441
  • 3
  • 18
  • 5
349
votes
11 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
  • 17,891
  • 17
  • 66
  • 87
252
votes
13 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
  • 10,738
  • 14
  • 59
  • 67
182
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,764
  • 8
  • 29
  • 26
179
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
  • 9,272
  • 11
  • 47
  • 65
171
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
  • 10,638
  • 10
  • 41
  • 56
171
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…
162
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,785
  • 2
  • 24
  • 37
143
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
  • 36,899
  • 45
  • 162
  • 260
132
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
  • 23,397
  • 16
  • 69
  • 92
124
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
  • 10,154
  • 13
  • 52
  • 66
120
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(){…
Moeb
  • 10,527
  • 31
  • 84
  • 110
102
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