Questions tagged [multiple-inheritance]

52 questions
152
votes
9 answers

Is there any "real" reason multiple inheritance is hated?

I've always liked the idea of having multiple inheritance supported in a language. Most often though it's intentionally forgone, and the supposed "replacement" is interfaces. Interfaces simply do not cover all the same ground multiple inheritance…
66
votes
1 answer

How are mixins or traits better than plain multiple inheritance?

C++ has plain multiple inheritance, many language designs forbid it as dangerous. But some languages like Ruby and PHP use strange syntax to do the same thing and call it mixins or traits. I heard many times that mixins/traits are harder to abuse…
Gherman
  • 945
  • 1
  • 7
  • 13
26
votes
9 answers

What is different between the internal design of Java and C++ that lets C++ have multiple inheritance?

It's drilled into the newbie Java programmers that Java (pre-Java 8) has no multiple class inheritance, and only multiple interface inheritance, because otherwise you run into diamond inheritance problem (Class A inherits from classes B and C, both…
DVK
  • 3,576
  • 2
  • 24
  • 28
23
votes
7 answers

Does multiple inheritance violate Single Responsibility Principle?

If you have a class which inherits from two distinct classes, does not this mean that your subclass automatically does (at least) 2 things, one from each superclass? I believe there is no difference if you have multiple interface inheritance. To be…
21
votes
2 answers

How do Traits in Scala avoid the "diamond error"?

(Note: I used 'error' instead of 'problem' in the title for obvious reasons.. ;) ). I did some basic reading on Traits in Scala. They're similar to Interfaces in Java or C#, but they do allow for default implementation of a method. I was wondering:…
Aviv Cohn
  • 21,190
  • 31
  • 118
  • 178
19
votes
1 answer

Why not make a language with mixin-only inheritance?

It seems that in all class-based or prototypal OOP languages, mixins are either an afterthought or a secondary feature. However, to me it looks like traditional inheritance is just a specific case of using a single mixin. I am assuming this is…
Den
  • 4,827
  • 2
  • 32
  • 48
17
votes
8 answers

Why is "diamond problem" a problem? Why doesn't the child simply call both parent's methods sequentially? Why is a thing with solutions a problem?

I know there have been many post about diamond problem, one of it: Why do you reduce multiple inheritance to the diamond problem?. But I'm not asking what it is or what is the solution of the problem. Also I'm not asking why multiple inheritance is…
wcminipgasker2023
  • 721
  • 1
  • 3
  • 10
17
votes
2 answers

Should a trait refer to parent methods?

Is it a code smell if the methods in my trait refer to parent::methods or to methods that are assumed to be in the utilising class? A random (senseless) example trait foo { public function bar() { return parent::bar() . 'baz'; …
Kamafeather
  • 335
  • 1
  • 2
  • 7
16
votes
5 answers

Proper workaround for multiple inheritence in Java (Android)

I have a conceptual problem with a proper implementation of code which seems require multiple inheritance, that would not be a problem in many OO languages, but as the project is for Android, there is no such thing like multiple extends. I have a…
Stan
  • 271
  • 1
  • 2
  • 7
15
votes
7 answers

Multiple inheritance use cases

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…
treecoder
  • 9,475
  • 10
  • 47
  • 84
13
votes
4 answers

Parallel hierarchies - partly same, partly different

There are quite a few similar questions out there 1, 2, 3, 4, but non seems exactly the case in this question, nor do the solutions seem optimal. This is a general OOP question, assuming polymorphism, generics, and mixins are available. The actual…
13
votes
2 answers

How does C++ handle multiple inheritance with a shared common ancestor?

I'm not a C++ guy, but I'm forced to think about this. Why is multiple inheritance possible in C++, but not in C#? (I know of the diamond problem, but that's not what I'm asking here). How does C++ resolve the ambiguity of identical method…
Sandeep
  • 241
  • 2
  • 6
11
votes
1 answer

Using Python's Method Resolution Order for Dependency Injection - is this bad?

I watched Raymond Hettinger's Pycon talk "Super Considered Super" and learned a little bit about Python's MRO (Method Resolution Order) which linearises a classes "parent" classes in a deterministic way. We can use this to our advantage, like in the…
11
votes
4 answers

Alternatives to multiple inheritance for my architecture (NPCs in a Realtime Strategy game)?

Coding isn't that hard actually. The hard part is to write code that makes sense, is readable and understandable. So I want to get a better developer and create some solid architecture. So I want to do create an architecture for NPCs in a…
Brettetete
  • 299
  • 3
  • 10
9
votes
2 answers

Is Python's inheritance an "is-a" style of inheritance or a compositional style?

Given that Python allows for multiple inheritance, what does idiomatic inheritance in Python look like? In languages with single inheritance, like Java, inheritance would be used when you could say that one object "is-a" of an another object and you…
Iain
  • 450
  • 2
  • 10
1
2 3 4