Multiple Inheritance in java

前端 未结 8 1399
無奈伤痛
無奈伤痛 2020-11-27 21:27

Java is not allowing inheritance from multiple classes (still it allows inheritance from multiple interfaces.), I know it is very much inline with classic diamond problem. B

相关标签:
8条回答
  • 2020-11-27 21:39

    Simplicity. To quote Tom Sintes,

    The Java design team strove to make Java:

    • Simple, object oriented, and familiar
    • Robust and secure
    • Architecture neutral and portable
    • High performance
    • Interpreted, threaded, and dynamic

    The reasons for omitting multiple inheritance from the Java language mostly stem from the "simple, object oriented, and familiar" goal. As a simple language, Java's creators wanted a language that most developers could grasp without extensive training. To that end, they worked to make the language as similar to C++ as possible (familiar) without carrying over C++'s unnecessary complexity (simple).

    In the designers' opinion, multiple inheritance causes more problems and confusion than it solves. So they cut multiple inheritance from the language (just as they cut operator overloading). The designers' extensive C++ experience taught them that multiple inheritance just wasn't worth the headache.

    0 讨论(0)
  • 2020-11-27 21:39

    It is true that Java did not use to support multiple inheritance of implementation (just of type i.e. interface). That was a design decision.

    However, since Java 8, it supports multiple inheritance using default methods. See http://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html:

    Multiple inheritance of implementation is the ability to inherit method definitions from multiple classes. Problems arise with this type of multiple inheritance, such as name conflicts and ambiguity. ... Default methods introduce one form of multiple inheritance of implementation.

    0 讨论(0)
  • 2020-11-27 21:41

    if java support multiple inheritance then it may effect other features of java
    consider super() method which is used to call super class constructor.if program has multiple super class(because of multiple inheritance) then compiler will get confused about which super class constructor should be called and throw an error

    0 讨论(0)
  • 2020-11-27 21:51

    It was a design decision of Java. You'll never get it, so don't worry too much about it. Although MI might help you make Mixins, that's the only good MI will ever do you.

    0 讨论(0)
  • 2020-11-27 21:51

    One simple answer is that all classes in Java derive from java.lang.Object IIRC. So, you would always have a diamond problem... :-D

    0 讨论(0)
  • 2020-11-27 21:57

    I have read that most programmers don't use multiple inheritance in a proper way. "Just go ahead and inherit from a class just to reuse code" is not the best practice in case of multiple inheritance.

    Many programmers do not know when to use simple inheritance in most cases. Multiple inheritance must be used with caution and only if you know what you are doing if you want to have a good design.

    I don't think that the lack of multiple inheritance in java (as in c++) will put restrictions in your code / application design / problem domain mapping into classes.

    0 讨论(0)
提交回复
热议问题