Diamond-Problem

The_diamond_problem (钻石问题)

http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem

The "diamond problem" (sometimes referred to as the "deadly diamond of death"[6]) is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and/or C has overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?

For example, in the context of GUI software development, a classButtonmay inherit from both classesRectangle(for appearance) andClickable(for functionality/input handling), and classesRectangleandClickableboth inherit from theObjectclass. Now if theequalsmethod is called for aButtonobject and there is no such method in theButtonclass but there is an overriddenequalsmethod inRectangleorClickable(or both), which method should be eventually called?

It is called the "diamond problem" because of the shape of the class inheritance diagram in this situation. In this case, class A is at the top, both B and C separately beneath it, and D joins the two together at the bottom to form a diamond shape.

Avoid the Diamond Problem when Using Inheritance in Loops

http://www.devx.com/tips/Tip/32035

While multiple inheritance is an extremely useful feature—escpecially in large scale software, it can also lead to a problem known as the DDD, or "Dreadful Diamond on Derivation," or simply "The Diamond Problem," shown in the following code:

class ElectricAppliance{
  int voltage,
  public:
  int getVoltage ( ) const { return voltage; }
} ;

class Radio : public ElectricAppliance { / * ...* / } ;

class Tape : public ElectricAppliance { / * ...* / } ;

class RadioTape: public Radio, public Tape { / * ..* / } ;

int main( )
{
 RadioTape rt ;
 int voltage = rt.getVoltage( ) ;
  /* Error - ambiguous call. Two  copies getVoltage( ) exist in rt*/
}

RadioTape is derived simultaneously from two base classes, each of which has its own copy of the methods and data members of ElectricAppliance. Consequently, the object rt has two ElectricAppliance subobjects. In cases like this, where you need to reduplicate data methods from a common base class, you should use virtual inheritance:

class Radio : virtual public ElectricAppliance { / * ..* / } ;
class Tape : virtual public ElectricAppliance { / * ..* / } ;
class RadioTape: public Radio, public Tape { / * ..* / }

Now, class RadioTape contains a single shared instance of ElectricAppliance, so there are no ambiguities:

int voltage = rt .getVoltage( ) ; //OK

Note:
Because of the virtual keyword in the base-class portion of Radio and Tape, an instance of ElectricAppliance will have have only a single, base subobject. This eliminates the ambiguities.

How interface resolves diamond problem in java?

Java allows the multiple inheritance of interfaces only. Interfaces are essentially abstract base classes with all abstract methods and no data members.

The diamond problem is therefore avoided since there is always only one implementation of a specific method or property and no ambiguity arises.


时间: 2024-11-05 19:28:34

Diamond-Problem的相关文章

关于Java中继承和接口的理解

关于Java中继承和接口的理解 Java语言中,为了实现代码重用,设计了继承这一机制,但是,其设计成单继承,这样设计是有原因的,如下图: Figure1:deadly diamond of death 此图问题称为菱形问题(diamond problem),就是说,当A的子类B和C同时实现了A中的方法,则同时继承了B和C的子类D在调用该方法时会出现混乱,无法得知该调用哪一个方法. 既然不能实现多继承,我们就会考虑把很多方法就写在父类里,或者继承抽象类,实现其方法,但是,这样会导致一个问题,比如说

面向对象编程中设计模式学习笔记

上学期学的OOP,最近把期末复习笔记拿出来温习,共享一发. Polymorphism means many different form If you have inheritance, you have polymorphism Inheritance Polymorphism Abstraction Encapsulation Information hiding Loose coupling Hello.exe (an executable) Hello.dll (an assembly

Java8接口中的默认方法

Java8新增特性,可以为接口中添加默认方法,实现这个接口的所有类都会继承这个方法,这样看起来,接口和类的界限就有点不明显了,同时也会带来多继承,菱形问题.这样设计的初衷是什么? 重所周知,java8开始支持lambda表达式,可以把函数当做参数传递,最明显的lambda表达式应用场景莫过于对collection的每一个元素应用lambda.如果想为Collection实现lambda表达式:list.forEach(…); // 这就是lambda代码 首先想到的是为Collection的父接

JI_4

(1) OOP and Design Patterns (1.1) Please explain difference among class, interface and abstract class. When you would use it rather than other? Feature Interface Abstract class Multiple inheritance A class may implement multiple interfaces. A class m

java中的默认方法

在[0.3.1 Java简介]中,有这么一段话:“请注意:Java并非作为教学语言设计的.世界各地的大学在讲授Java的过程中均遇到一些教学上的困难(如Java语言和API的快速升级),这些困难是计算机科学教育中一般性的挑战.” Java8中引入的默认方法,充分展示了Java平台概念的一致性与JDK向前兼容之间的矛盾,而且以牺牲概念的一致性而满足JDK向前兼容. 1.理想与现实 [曾经]Java接口纯粹是契约的集合,是一种程序设计的表达方式.从数据抽象的角度看,能够在不定义class的同时又可以

Design Pattern: Not Just Mixin Pattern

Brief 从Mix-In模式到Mixin模式,中文常用翻译为“混入/织入模式”.单纯从名字上看不到多少端倪,而通过采用Mixin模式的jQuery.extend我们是否可以认为Mixin模式就是深拷贝的代名词呢? 本文试图从继承机制入手对Mixin模式进行剖析,若有纰漏请大家指正,谢谢. The Fact of Inheritance     首先让我们一起探讨一下继承机制吧.作为OOP的三个重要特性(Inheritance,Polymorphism,and Encapsulation)之一,

KIP-382: MirrorMaker 2.0

Status Motivation Public Interfaces Proposed Changes Remote Topics, Partitions Aggregation Cycle detection Config, ACL Sync Internal Topics Remote Cluster Utils MirrorClient Replication Policies and Filters Connector Configuration Properties Example

python学习28——继承与派生

一 继承介绍 继承是一种创建新类的方式,在Python中,新建的类可以继承一个或多个父类,新建的类可称为子类或派生类,父类又可称为基类或超类 class ParentClass1: #定义父类 pass class ParentClass2: #定义父类 pass class SubClass1(ParentClass1): #单继承 pass class SubClass2(ParentClass1,ParentClass2): #多继承 pass 通过类的内置属性__bases__可以查看类

多继承带来的菱形问题

大多数面向对象语言都不支持多继承,而在Python中,一个子类是可以同时继承多个父类的,这固然可以带来一个子类可以对多个不同父类加以重用的好处,但也有可能引发著名的 Diamond problem菱形问题(或称钻石问题,有时候也被称为"死亡钻石"),菱形其实就是对下面这种继承结构的形象比喻 这种继承结构下导致的问题称之为菱形问题:如果A中有一个方法,B和/或C都重写了该方法,而D没有重写它,那么D继承的是哪个版本的方法:B的还是C的?如下所示 class A(object):    d