Multiple inheritance on Java interfaces

后端 未结 5 2013
暖寄归人
暖寄归人 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:19

    Implementing interfaces is not "inheritance", which is when you extend a class.

    Implementing interfaces is used to declare that a class "looks like" something, whereas extending classes is used to declare that a class "is a" something.

    It's OK to "look like" multiple things, but not "be" multiple things.


    There's nothing wrong with having empty interfaces that extend multiple interfaces as a way of collecting a set of interfaces into a single interface to convey a broader, but reused, API.

提交回复
热议问题