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

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

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

18条回答
  •  甜味超标
    2020-11-22 15:19

    Java supports multiple inheritance through interfaces only. A class can implement any number of interfaces but can extend only one class.

    Multiple inheritance is not supported because it leads to deadly diamond problem. However, it can be solved but it leads to complex system so multiple inheritance has been dropped by Java founders.

    In a white paper titled “Java: an Overview” by James Gosling in February 1995(link) gives an idea on why multiple inheritance is not supported in Java.

    According to Gosling:

    "JAVA omits many rarely used, poorly understood, confusing features of C++ that in our experience bring more grief than benefit. This primarily consists of operator overloading (although it does have method overloading), multiple inheritance, and extensive automatic coercions."

提交回复
热议问题