Why is Multiple Inheritance not allowed in Java or

2019-01-01 03:43发布

I know that multiple inheritance is not allowed in Java and C#. Many books just say, multiple inheritance is not allowed. But it can be implemented by using interfaces. Nothing is discussed about why it is not allowed. Can anybody tell me precisely why it is not allowed?

17条回答
明月照影归
2楼-- · 2019-01-01 04:10

Back in the old days ('70s) when Computer Science was more Science and less mass production the programmers had time to think about good design and good implementation and as a result the products (programms) had high quality ( eg. TCP/IP design and implementation ). Nowadays, when everybody is programming, and the managers are changing the specs before deadlines, subtle issues like the one descriped in the wikipedia link from Steve Haigh post are difficult to track; therefore, the "multiple inheritance" is limited by compiler design. If you like it, you can still use C++ .... and have all the freedom you want :)

查看更多
看风景的人
3楼-- · 2019-01-01 04:11

Dynamic loading of classes makes the implementation of multiple inheritance difficult.

In java actually they avoided the complexity of multiple inheritance instead by using single inheritance and interface. Complexity of multiple inheritance is very high in a situation like below explained

diamond problem of multiple inheritance. We have two classes B and C inheriting from A. Assume that B and C are overriding an inherited method and they provide their own implementation. Now D inherits from both B and C doing multiple inheritance. D should inherit that overridden method, jvm can't able to decide which overridden method will be used?

In c++ virtual functions are used to handle and we have to do explicitly.

This can be avoided by using interfaces, there are no method bodies. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

查看更多
孤独总比滥情好
4楼-- · 2019-01-01 04:16

The main (although by no means the only) reason people steer away from MI is the so called "diamond problem" leading to ambiguity in your implementation. This wikipedia article discusses it and explains better than I could. MI can also lead to more complex code, and a lot of OO designers claim that you do't need MI, and if you do use it your model is probably wrong. I'm not sure I agree with this last point, but keeping things simple is always a good plan.

查看更多
梦寄多情
5楼-- · 2019-01-01 04:16

I take the statement that "Multiple inheritance is not allowed in Java" with a pinch of salt.

Multiple Inheritance is defined when a "Type" inherits from more than one "Types". And interfaces are also classified as types as they have behavior. So Java does have multiple inheritance. Just that it is safer.

查看更多
旧人旧事旧时光
6楼-- · 2019-01-01 04:17

In C++ multiple inheritance was a major headache when used improperly. To avoid those popular design issues multiple interfaces "inheritance" was forced instead in modern languages (java, C#).

查看更多
流年柔荑漫光年
7楼-- · 2019-01-01 04:17

Another reason is that single-inheritance makes casting trivial, emitting no assembler instructions (other than checking for the compatibility of the types where required). If you had multiple-inheritance, you'd need to figure out where in the child class a certain parent starts. So performance is certainly a perk (although not the only one).

查看更多
登录 后发表回答