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

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

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

18条回答
  •  萌比男神i
    2020-11-22 15:14

    For the same reason C# doesn't allow multiple inheritence but allows you to implement multiple interfaces.

    The lesson learned from C++ w/ multiple inheritence was that it lead to more issues than it was worth.

    An interface is a contract of things your class has to implement. You don't gain any functionality from the interface. Inheritence allows you to inherit the functionality of a parent class (and in multiple-inheritence, that can get extremely confusing).

    Allowing multiple interfaces allows you to use Design Patterns (like Adapter) to solve the same types of issues you can solve using multiple inheritence, but in a much more reliable and predictable manner.

提交回复
热议问题