为什么 Java 允许从接口而不是从抽象/具体类的多重继承
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3008683/
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 does Java allow multiple inheritance from interfaces but not from abstract/concrete classes
提问by dan
Possible Duplicate:
Why there is no multiple inheritance in Java, but implementing multiple interfaces is allowed
Why Java allows multiple inheritance from interfaces but not from abstract or concrete classes
为什么 Java 允许从接口而不是抽象或具体类的多重继承
回答by SLaks
Multiple inheritance of concrete classes raises a variety of issues.
具体类的多重继承引发了各种问题。
For example, what if a class inherits two different implementations of the same method from two different base classes?
例如,如果一个类从两个不同的基类继承同一方法的两个不同实现怎么办?
To avoid these issues, Java doesn't support this feature.
Unlike concrete classes, interfaces cannot have method bodies.
为了避免这些问题,Java 不支持此功能。
与具体类不同,接口不能有方法体。
Therefore, none of these issues apply to interfaces.
因此,这些问题都不适用于接口。
回答by Uri
I really don't like the term "inherit" here, it leads to a lot of confusion.
我真的不喜欢这里的“继承”这个词,它会导致很多混乱。
Java only allows interfaces to extend other interfaces, and for classes to implement interfaces.
Java 只允许接口扩展其他接口,并允许类实现接口。
If you look at an interface as a mathematical set of declarations, then each "extends" merely provides the union of the set from the superinterface and that of the current interface. You are therefore allowed to do multiple "unions".
如果您将接口视为一组数学声明,那么每个“扩展”仅提供来自超接口的集合与当前接口的集合的并集。因此,您可以进行多个“联合”。
When you eventually get to a class that implements one or more interfaces, the semantics here are merely that the class must provide implementations for all the methods in the set. A class implementing multiple interfaces could be rewritten as a class implementing a single interface that extends all the above interfaces.
当您最终获得一个实现一个或多个接口的类时,这里的语义仅仅是该类必须为集合中的所有方法提供实现。实现多个接口的类可以重写为实现扩展所有上述接口的单个接口的类。
In the case of classes inheriting multiple classes it is not allowed because it leads to a variety of problems, including the diamond problem. For instance, if I have two supertypes with different implementations of the same method signature, which one should be used in the subtype?
在类继承多个类的情况下,这是不允许的,因为它会导致各种问题,包括菱形问题。例如,如果我有两个具有相同方法签名的不同实现的超类型,那么应该在子类型中使用哪一个?
回答by Nikita Rybak
To make language simpler and more elegant. C++ allows a lots of stuff, but it's often pain to learn and use. We (me, at least :)) don't want java to be like that.
让语言更简单、更优雅。C++ 允许很多东西,但学习和使用通常很痛苦。我们(我,至少 :))不希望 java 变成那样。
回答by Robin
Because implementing an interface is not inheritance. It simply means that your class will adhere to a predefined contract, typically to provide a set of methods related to a certain functionality. Any class can adhere to many such contracts without conflict (unless two of those interfaces define the same method).
因为实现接口不是继承。它只是意味着您的类将遵守预定义的契约,通常是提供一组与特定功能相关的方法。任何类都可以遵守许多这样的契约而不会发生冲突(除非其中两个接口定义了相同的方法)。
Unlike inheritance, it does not automagically receive attributes or functionality due to a hierarchical relationship with its superclass since no such relationship exists.
与继承不同,它不会因为与其超类的层次关系而自动接收属性或功能,因为不存在这种关系。
Multiple inheritance is basically not allowed in Java or many other OO languages due to the already mentioned Diamond Inheritance problem.
由于已经提到的 Diamond Inheritance 问题,Java 或许多其他 OO 语言中基本上不允许多重继承。