Multiple Inheritance: The Problem

Although multiple inheritance is allowed in some languages, it can lead to some serious problems. For instance, it can give rise to an ambiguous situation called the Diamond Problem. Below is a short description of what the Diamond Problem is:

In object-oriented programming languages with multiple inheritance, the diamond problem (sometimes referred to as the "deadly diamond of death") is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If D calls a method defined in A (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C? (Wikipedia)

 

The Diamond Problem refers to a specific problem - ambiguity in method-calling - that can arise out of multiple inheritance. This point may lead some to believe that if we can somehow resolve the method-calling issue, then multiple inheritance acceptable. But, that is not the case. In a strict object-oriented perspective, the problem with multiple inheritance can be established at a more general level - treating the matter at a conceptual level.

Consider the following inheritance relationship between class A and class B.



 Class B is a subclass of class A. As such, class B inherits properties of class A. But, class B does not just inherits a set of attributes and methods, it also inherits the type of A. Any object of type B is also of type A. That is, being a subclass of a superclass also means having the type of the superclass. It is in this sense that inheritance is said to be a is-a relationship. B is-a A.


Consider the above inheritance relationship. Because Cat is a subclass of Animal, any cat is also an Animal. Once this relationship exists, through inheritance, then a cat cannot be not an Animal. Being an Animal is inherent in being a Cat.

If multiple inheritance is allowed, then we can have cases such as the following.




But, does it make sense to have these cases? Are they meaningful? Well, obviously not. A cat cannot be both an Animal and a Plant at the same time. The second child class cannot be both Hot and Cold at the same time.

Hopefully the nature of problem is revealing itself. If so, we can now present a general formulation of the problem in the following argument.

Let’s suppose multiple inheritance is allowed. Let’s further suppose that there classes A and NotA with mutually exclusive sets of properties, and a third class B inheriting from both of them. Since, inheritance is a is-a relationship (type inheritance), then the following two statements must hold about these three classes:

a) B is-a A.
b) B is-a NotA.


But, these two statements cannot be true at the same time since A and NotA are mutually exclusive. Hence, we can conclude that if Type-Inheritance is true, then multiple inheritance can lead to impossible situation.

Add comment


Security code
Refresh

Additional information