Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?

后端 未结 18 2092
囚心锁ツ
囚心锁ツ 2020-11-22 14:55

Java doesn\'t allow multiple inheritance, but it allows implementing multiple interfaces. Why?

18条回答
  •  面向向阳花
    2020-11-22 15:41

    Java does not support multiple inheritance because of two reasons:

    1. In java, every class is a child of Object class. When it inherits from more than one super class, sub class gets the ambiguity to acquire the property of Object class..
    2. In java every class has a constructor, if we write it explicitly or not at all. The first statement is calling super() to invoke the supper class constructor. If the class has more than one super class, it gets confused.

    So when one class extends from more than one super class, we get compile time error.

提交回复
热议问题