Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does multiple inheritance come in handy? [duplicate]

Can you provide some real-world examples when problem can be more easily addressed using multiple inheritance rather than using composition or other alternatives?

When should one use multiple inheritance?

Why do some languages support multiple inheritance (C++, Python) and others don't (Java, Ruby)? I mean - based on what factors do creators of programming languages decide to include support for MI or not.

like image 929
Luke Avatar asked Jul 17 '15 14:07

Luke


People also ask

When multiple inheritance concept should be used?

Multiple inheritance is useful when a subclass needs to combine multiple contracts and inherit some, or all, of the implementation of those contracts. For example, the AmericanStudent class needs to inherit from both the Student class and the American class.

Does multiple inheritance lead to ambiguity?

The ambiguity that arises when using multiple inheritance refers to a derived class having more than one parent class that defines property[s] and/or method[s] with the same name. For example, if 'C' inherits from both 'A' and 'B' and classes 'A' and 'B', both define a property named x and a function named getx().

What is the purpose of multiple inheritance in C++?

Multiple Inheritance in C++ Multiple inheritance occurs when a class inherits from more than one base class. So the class can inherit features from multiple base classes using multiple inheritance. This is an important feature of object oriented programming languages such as C++.

Does Javasupport multiple inheritance?

Multiple inheritance in java Therefore, in Java multiple inheritance is not allowed and, you cannot extend more than one other class. Still, if you try to do so, a compile-time error is generated.


2 Answers

Multiple inheritance is mostly used for integration purposes, i.e. when you need an object that implements all the required functionality of the classes it derives from. Whether this amounts to a "better" solution than possible alternatives, is a matter of debate or taste, so you're probably lucky that this question wasn't closed as primarily opinion-based.

With regard to examples,

  • this article on using Python's super() for multiple inheritance shows an example of implementation sharing. You can't have this with interfaces or composition,
  • a similar example is explained on the Wikipedia page on multiple inheritance for deriving Button from Rectangle and Clickable (it also has a long list of languages supporting MI),
  • an answer to a previous question for good MI examples points to Observable and the like, which at least Bertrand Meyer claims to be cleaner implemented with MI than without (hence you're also lucky that this question didn't get closed as a duplicate).

From personal experience, multiple inheritance can be quite helpful, but it can also be a big problem easily. The Wikipedia page discusses the Diamond problem to some extent: it boils down to the question what should happen if two or more base classes provide an implementation of some method. Languages need to define / implement a way to deal with this, typically by defining some method resolution order (e.g. Python's mro).

The conflict potential increases, of course, with the number of base classes and the number of methods. I had a case once where the framework (implemented in Python) we were using employed multiple inheritance of a handful of base classes for some class we had derived from. We could then happily override an inherited method without being aware of it.

Multiple inheritance, while sometimes useful, can be seen as a violation of the Single responsibility principle: by definition, a class deriving from multiple base classes will behave like either class. It's hence also quite likely that from a data modelling perspective, the Liskov substitution principle is also violated.

So, I believe that creators of programming languages might recognize that multiple inheritance is seen as not free of conceptual issues and might require substantial implementation effort while providing only limited added value over other solutions -- but that's just my personal guess.

like image 184
schaueho Avatar answered Nov 19 '22 17:11

schaueho


Answering your last question first: The reason that some languages don't support multiple inheritance is because it is rarely necessary and tends to make the code much more complicated compared to composition or multiple interface inheritance.

This relates to your first two questions in that it is difficult to find a real-world example (at least, a simple one) where M.I. could solve a problem easily. Clearly, the creators of languages where M.I. is not supported think you should never use it. I would guess that the reason that some languages do support it is because the creators saw no reason not to.

Here is a link to a frequently asked question on MSDN that explains why C# doesn't support multiple inheritance.

like image 40
Matt Martin Avatar answered Nov 19 '22 17:11

Matt Martin