Why Java does not support multiple inheritance

Source: Internet
Author: User

multiple inheritance refers to a subclass that can inherit from more than one parent class at the same time, and thus have characteristics of more than one parent class, but the disadvantage is significant .

1. If the subclass inherits the same member variable in the parent class, the child class will not be able to discriminate which parent class is used when referencing the variable. such as:

public class ClassA//Parent class ClassA

{

private int num = 0;

}

public class ClassB//Parent class ClassB

{

private int num = 1;

}

public class ClassC extends CLASSA,CLASSB//subclass ClassC inherit from ClassA and ClassB

{

public static void Main (String [] args)

{

ClassC obj = new ClassC ();

Obj.print (); Call the parent class member variable num, that num equals 0 or 1?

}

public void print ()

{

System.out.println (Super.num);

}

}

2. If a subclass inherits multiple parent classes that have the same method, and the subclass does not overwrite the method (if overridden, the method is used directly in the subclass), then calling the method will not determine which parent class the method is called. such as:

public class ClassA//Parent class ClassA

{

public void Fun ()

{

System.out.print ("Hello");

}

}

public class ClassB//Parent class ClassB

{

public void Fun ()

{

System.out.print ("Hello");

}

}

public class ClassC extends CLASSA,CLASSB

{

public static void Main (String [] args)

{

ClassC t = new ClassC ();

T.print ();

}

public void print ()

{

Super.fun (); Call the fun method in the parent class, but since ClassA, ClassB have the fun () method, you will not be able to determine the use

Which method in the parent class

}

}

Therefore, Java only allows single inheritance, that is, a subclass can inherit only one parent class. But in order to extend the functionality of subclasses, Java uses interfaces to overcome the disadvantages of not using multiple inheritance.

An interface is a special abstract class in which member variables are defaulted to the static final type, which is constant, and the methods in the interface are abstract and have no method body.

The specific method can only be implemented by the implementation of the interface class, at the time of the call will always call the implementation of the method of the class (there is no ambiguity), so there is no more than the second disadvantage of inheritance, but because the interface has only static constants, but because the static variable is the compile time to determine the call relationship, Even if there is a certain conflict will be at compile-time prompt error, and reference static variables generally directly use the class name or interface name, so as to avoid ambiguity, so there is no more than the first disadvantage of inheritance. There is no such disadvantage in cases where an interface inherits multiple parent interfaces.

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.