简体   繁体   English

多重继承和接口

[英]Multiple Inheritance and Interfaces

A common " question to which the answer is no " is does Java support multiple inheritance? 一个常见的“ 答案是否定的问题 ”是Java支持多重继承吗?

I'm looking for some elaboration on how this rule is handled by the (Oracle) JVM; 我正在寻找有关(Oracle)JVM如何处理此规则的详细说明; more specifically: 进一步来说:

At runtime does the JVM have any notion of an Interface or does it just treat it like an abstract class that happens to not implement any methods? 在运行时,JVM是否具有接口的任何概念,或者只是将其像碰巧没有实现任何方法的抽象类一样对待?

In other words, would my code become this? 换句话说,我的代码会变成这个吗?

My Code: 我的代码:

public class A extends B implements C {

public interface C {

Compiles into: 编译成:

public class A extends B, C {

public abstract class C {

In which case, the JVM could be said to support multiple inheritance so long no more than one parent class implements methods. 在这种情况下,可以说JVM支持多种继承,只要不超过一个父类实现方法即可。

Or are interfaces more deeply woven into the guts of the JVM? 还是接口更深入地融入了JVM的内心?

At runtime, the JVM does have a notion of interfaces. 在运行时,JVM确实具有接口的概念。 Methods called through an interface type are invoked with invokeinterface . 通过接口类型调用的方法由invokeinterface调用。

Compiling interfaces into abstract classes would not work: 将接口编译成抽象类将不起作用:

  • A single method may indeed be implemented by more than one of the interfaces that a class implements. 单个方法的确可以由一个类实现的多个接口来实现。
  • An interface method may be invoked on more than one type that implements the interface. 接口方法可以在实现接口的一种以上类型上调用。
  • An interface provides no implementation for its methods. 接口不为其方法提供任何实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM