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

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

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

18条回答
  •  囚心锁ツ
    2020-11-22 15:24

    For example two class A,B having same method m1(). And class C extends both A, B.

     class C extends A, B // for explaining purpose.
    

    Now, class C will search the definition of m1. First, it will search in class if it didn't find then it will check to parents class. Both A, B having the definition So here ambiguity occur which definition should choose. So JAVA DOESN'T SUPPORT MULTIPLE INHERITANCE.

提交回复
热议问题