"Analysis: Why Java does not support multiple inheritance"

Source: Internet
Author: User

A long time ago, a good friend of bloggers gave me a question, why Java does not support multiple inheritance, if more inheritance will have what error. This problem has plagued me for a long time, I derive from the single inheritance, and from the multi-state level to think about, but can not explain why Java does not support multiple inheritance reasons, but also in the school to ask teachers, teachers are just let me think more, well, then I think more.

In 1995, when James Gosling, the designer of the Java language, published a white paper, he spoke of an idea: Why does Java not support multiple inheritance?

We start with the designer's definition of the Java language: Java: A simple, object-oriented, distributed, interpretive, robust, secure, architecture-neutral, portable, high-performance, multi-threaded, dynamic language. Compared to other programming languages such as C + +, Java eliminates a lot of headaches, such as pointers, multiple inheritance. This really brings a real sense to the Java programmer: The language is much simpler.

Let's return to the question: Why does Java not support multiple inheritance? Now I use the illustration to explain to you:

  

As shown, we have two classes B and C that inherit from a. Suppose B and C both rewrite inherited methods, and they provide their own implementations. Now B and C inherit multiple inheritance from D. D should inherit, override the method that will be used to override the method? Is it from B or C? Here, we have ambiguous. In C + + we may fall into this trap, but it also has its own mechanism to solve the problem, but Java does not need to consider this problem, because Java does not have multiple inheritance, So the question of diamond inheritance will never bother us (perhaps the Java designers have deliberately omitted the Diamond inheritance processing mechanism in order to avoid the problem of diamond inheritance).

From the point of contact with the Java language, this language has been compared with other languages, and multiple inheritance issues are considered to be one of the short boards in Java compared to other languages, and as a supplement, Java adds multiple implementations of the interface. Perhaps our Java will continue to be questioned because of multiple inheritance problems, but is it really a flaw to omit multiple inheritance? Perhaps this is one of the advantages that the Java language is recognized by more people!

"Analysis: Why Java does not support multiple inheritance"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.