Multiple inheritance on Java interfaces

后端 未结 5 2015
暖寄归人
暖寄归人 2020-12-09 08:28

I thought multiple inheritance was always illegal in Java, but this code compiles:

public interface A {
  void a();
}

public interface B {
  void b();
}

pu         


        
5条回答
  •  执念已碎
    2020-12-09 09:15

    An interface can extend one or more other interfaces. You can also implement more than one interface in your classes. It is legal because interface is only contract - there is no implementation. You're simply defining a contract for what a class is able to do, without saying anything about how the class will do it.

提交回复
热议问题