Multiple inheritance and diamond problems

Translated from John Demetriou's article "Multiple Inheritance And The Diamond Problem" on April 8, 2018 1

Insert picture description here

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:

Insert picture description here

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

Public Number: Technical Translation Station


  1. http://www.devsanon.com/language-agnostic/multiple-inheritance-and-the-diamond-problem/ Multiple Inheritance And The Diamond Problem ↩︎

  2. https://mp.weixin.qq.com/s/UaWxZHeYh4OQByNlyYX0cg C# 8: Default interface method↩︎

Guess you like

Origin blog.csdn.net/weixin_47498376/article/details/109208263