Java - Why multiple inheritances are not supported in Java
2015-07-01 11:15
495 查看
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来间接实现多重继承。
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来间接实现多重继承。
相关文章推荐
- Spring MVC拦截器+注解方式实现防止表单重复提交
- 《Java课程实习》日志(周二)
- JDBC初探
- 一、Spring源码分析——Spring框架
- java.sql.SQLException: Listener refused the connection with the following error:
- java集合框架知识总结
- LeetCode_71---Simplify Path
- 【Java EE 学习 24 上】【注解详解】
- 用java对文件进行分割与合并
- 《Java课程实习》日志(周一)
- java:数组复制和排序
- java.lang.IllegalStateException解决
- Spring Boot开发
- Java 集合总结
- Java继承
- Spring Boot开发
- java中如何将string 转化成long
- LeetCode_69---Sqrt(x)
- javascrip中setTimeout和setInterval
- Core Java读书笔记之String