StuPointerException
StuPointerException

Reputation: 7267

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?

Upvotes: 1

Views: 125

Answers (1)

Andy Thomas
Andy Thomas

Reputation: 86411

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.

Upvotes: 2

Related Questions