为什么java不支持多重继承
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3768084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why java does not support multiple inheritance
提问by Subhransu Mishra
Possible Duplicate:
Why there is no multiple inheritance in Java, but implementing multiple interfaces is allowed
hi all, I am new to java and when I am try to use multiple inheritance concept in java its showing a compile time error.Why java does not support it.I heard about interface key word but do not know why java support it directly. please help me in this and explain.
大家好,我是 java 新手,当我尝试在 java 中使用多重继承概念时,它显示编译时错误。为什么 java 不支持它。我听说过 interface 关键字,但不知道为什么 java 直接支持它。请帮我解释一下。
采纳答案by Colin Hebert
Multiple inheritance can be really difficult to understand. When you have a multiple inheritance with two classes which have methods in conflicts, how do you handle this ?
多重继承真的很难理解。当您有两个具有冲突方法的类的多重继承时,您如何处理?
Of course solutions exist (in C++ for example) but the creators of Java decided it was way to complicated and not really in the Java philosophy (make development a lot easier).
当然,解决方案是存在的(例如在 C++ 中),但 Java 的创建者认为它是一种复杂的方式,而不是真正符合 Java 哲学(使开发更容易)。
From sun.com :
来自太阳网:
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 中被抛弃了。多继承的理想特性由接口提供——在概念上类似于目标 C 协议。
接口不是类的定义。相反,它是一个或多个类将实现的一组方法的定义。接口的一个重要问题是它们只声明方法和常量。变量不能在接口中定义。
Resources :
资源 :
回答by IProblemFactory
Because, it was hard to use it. Instead, Java has interface
what is much better solutions.
因为,很难用。相反,Java 有interface
更好的解决方案。
回答by AlcubierreDrive
The main problem with multiple inheritance (alluded to by Colin and Rin) is known is The Diamond Problem.
已知多重继承的主要问题(由 Colin 和 Rin 提及)是钻石问题。
I quote:
我引用:
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?
菱形问题是当两个类 B 和 C 继承自 A,而类 D 继承自 B 和 C 时出现的歧义。和 C 以不同的方式覆盖了该方法,那么它从哪个类继承:B 还是 C?