Multiple inheritance in Java and multiple inheritance issues in C + +

Source: Internet
Author: User
Tags multiple inheritance in java

First look at the problem of multiple inheritance, inheritance is prone to lead to the Diamond Crisis (also known as the prism problem ), a picture to explain:

Suppose there is a public method fun () in Class A, and Class B and Class C inherit Class A at the same time, in Class B or Class C, each of the method fun () cover , then Class D inherits the Class B and Class C through multiple inheritance, which leads to the diamond crisis, how to judge the method when the program is running?

Summarized as follows, there are two main aspects:

(1) If you have an instance variable of the same name in more than one parent class that inherits from a subclass, the subclass will be ambiguous when referencing the variable, and it is not possible to determine which parent class's variable should be used. (2) If you have the same method in more than one parent class that inherits from a subclass, and there are no overrides for the method in the subclass, then calling the method will create ambiguity and cannot determine which parent class should be called. There can be no instance variables in an interface, only static constants, no concrete methods (including method bodies), only abstract methods, and therefore, the disadvantage of multiple inheritance is discarded. For a class to implement multiple interfaces, because the interface only the abstract method, the concrete method can only be implemented by the implementation of the interface class, at the time of invocation will always call the implementation of the method of the class (there is no ambiguity), so there is no second disadvantage of multiple inheritance, and because the interface has only static constants, However, since static variables determine the invocation relationship at compile time, even if there is a certain conflict, there will be an error in compiling, whereas reference static variables generally use the class name or interface name directly, thus avoiding ambiguity and therefore there is no first disadvantage of multiple inheritance. There is no such disadvantage in cases where an interface inherits multiple parent interfaces.

However, in Java, you can use interfaces and inner classes to indirectly achieve multiple inheritance effects:

This is the indirect implementation of multiple inheritance through interfaces.

Interface howeat{

Public abstract String howeat ();

}

Class Chenken implements howeat{

Public String howeat () {

Return "Chicken:fry it";

}

}

Class Orange implements howeat{

Public String howeat () {

Return "Orangr:make it Juice";

}

}

This is achieved through an internal class.

Class father{

public void output () {

System.out.println ("father");

}

}

Class mother{

public void output () {

System.out.println ("Mather");

}

}

Class son{

Class Father_son extends father{


}

Class Mother_son extends mother{


}

public void Father () {

(New Father_son ()). output ();

}

public void Mather () {

(New Mother_son ()). output ();

}

}


Multiple inheritance in Java and multiple inheritance issues in C + +

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.