为什么java不支持多重继承

why java does not support multiple inheritance

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
Why there is no multiple inheritance in Java, but implementing multiple interfaces is allowed

大家好,我是Java新手,当我尝试在Java中使用多重继承概念时,它显示了编译时错误。为什么Java不支持它。我听说过接口关键字,但是不知道为什么Java直接支持它。请帮我解释一下。


多重继承的主要问题(科林和林提到)是钻石问题。

我引述:

The diamond problem is an ambiguity
that arises when two classes B and C
inherit from A, and class D inherits
from both B and C. If a method in D
calls a method defined in A (and does
not override the method), and B and C
have overridden that method
differently, then from which class
does it inherit: B, or C?


多重继承可能真的很难理解。当您拥有两个类的多重继承,而这两个类有冲突的方法时,您如何处理这个问题?

当然,解决方案存在(例如C++),但Java的创建者认为这是复杂的,而不是真正的Java哲学(使开发变得更容易)。

来自太阳网:

Multiple inheritance--and all the problems it generates--was discarded from Java. The desirable features of multiple inheritance are provided by interfaces--conceptually similar to Objective C protocols.
An interface is not a definition of a class. Rather, it's a definition of a set of methods that one or more classes will implement. An important issue of interfaces is that they declare only methods and constants. Variables may not be defined in interfaces.

资源:

  • Java语言环境


因为它很难用。相反,Java有EDCOX1 0,什么是更好的解决方案。