Translated from John Demetriou's article "Multiple Inheritance And The Diamond Problem" on April 8, 2018 1
Before I start, I assume that everyone knows what inheritance is in object-oriented programming and what benefits it can provide. I will not delve into the basics of object inheritance. This article focuses more on multiple inheritance and the problems it faces.
Indeed, a big problem with multiple inheritance is the diamond problem. You may ask why? Imagine the distribution of inheritance as follows:
The figure above shows, D inherits from B and C , while B and C inherits from A .
Assuming now that A is an abstract class (not necessarily a problem exists diamond abstract class , but it makes the problem more pronounced), and comprises a called Jump()
public abstract method ( public abstract Jump()
). Both B and C need to implement this method in their own specific way. Then D inherits from two classes, when someone in D call on Jump()
when what will happen?
Call Jump()
which implementation is unclear!
For this reason, both Java and C# do not allow multiple inheritance. But they allow multiple interface inheritance, and a new feature of C # interface linked to multiple inheritance issue 2 . We will next discuss the property.
Author: John Demetriou
Translator: Technical Zemin
Publisher: Technical Verses
links: English text
http://www.devsanon.com/language-agnostic/multiple-inheritance-and-the-diamond-problem/ Multiple Inheritance And The Diamond Problem ↩︎
https://mp.weixin.qq.com/s/UaWxZHeYh4OQByNlyYX0cg C# 8: Default interface method↩︎