Diamond Problem

Diamond problem occurs with multiple inheritance where a derived class derives from two intermediate base classes whereas those base classes have a common base class. When compiled a common method in super base class call will resolve to ambiguity and throw an compiler error. This can be solved by adding virtual keyword in both of the base classes which creates separate vtables. By using these vtables runtime binding is resolved. (In other words both vtables of intermediate base classes will be referencing to the address of function in super base class)

In C# this problem is solved my completely blocking multiple inheritance. Only multi-level implementation is permitted in C#. Also note that multiple interface implementation is supported in C#. The reason is multiple inheritance leads to more complexity. (only one of which is diamon problem) So the .NET architects kept away from it.

No comments:

Post a Comment