Java - Why multiple inheritances are not supported in Java

来源:互联网 发布:酒店订房系统 php源码 编辑:程序博客网 时间:2024/05/16 19:28

http://javarevisited.blogspot.de/2011/07/why-multiple-inheritances-are-not.html

Sometimes I can't open this page without proper proxy, so I have to copy the content here.


1) First reason is ambiguity around Diamond problem. Consider a class A has foo() method and then B and C derived from A and has their own foo() implementation and now class D derive from B and C using multiple inheritance. And if we refer just foo(), compiler will not be able to decide which foo() it should invoke. This is also called Diamond problem because structure on this inheritance scenario is similar to 4 edge diamond, see below

           A foo()

           / \

          /   \

   foo() B     C foo()

          \   /

           \ /

            D

           foo()

2) Second and more convincing reason to me is that multiple inheritances does complicate the design and creates problem during casting, constructor chaining etc. 没有多少场景需要多重继承。 可以通过实现Interface来间接实现多重继承。


0 0
原创粉丝点击