简体   繁体   中英

Multiple Inheritance and Interfaces

A common " question to which the answer is no " is does Java support multiple inheritance?

I'm looking for some elaboration on how this rule is handled by the (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?

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.

Or are interfaces more deeply woven into the guts of the JVM?

At runtime, the JVM does have a notion of interfaces. Methods called through an interface type are invoked with 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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