Blog

The Diamond Problem

Interesting

An interesting problem that can arise in the use of multiple inheritance.

Old Article

This content may be out of date or broken, please take care.

Today I was refreshing my knowledge on classes – specifically Abstract Classes and Interfaces. One of the best descriptions of an interface I have read is when it is described as a “contract for classes”, whereby the subclass implementing the interface must implement all classes given in the interface.

Contrast to this is an abstract class, where only abstract methods in the abstract class are required. As an abstract class is extended, only on abstract class can be extended by a concrete class – multiple interfaces can, however be implemented. Similarly to an interface, neither can be instantiated and are used to create reuse of code.

I digress, the actual thing that I learnt from this was the diamond problem. This problem arises from ambiguity in inheritance of classes and occurs when classes B and C extend A. Then another class D inherits from B and C creating this diamond like effect.

The problem that occurs from this multiple inheritance bonaza, is what happens to our functions in D that have not been overidden? Languages appear to deal with it in different ways, and you can read all about it in this Wiki article.

Comments