Home > Java > javaTutorial > body text

The reason why java does not have multiple inheritance

(*-*)浩
Release: 2019-05-21 11:54:49
Original
2591 people have browsed it

Java only supports single inheritance and does not allow multiple inheritance. Single inheritance means that a class can only have one parent class, and multiple inheritance means that a class can have multiple parent classes.

The reason why java does not have multiple inheritance

Java can have multiple levels of inheritance, that is, a class can inherit a subclass of a certain class. For example, class B inherits class A, and class C can inherit class B. , then class C also indirectly inherits class A

The subclass can inherit all the member variables and methods of the parent class, but the subclass can never inherit the constructor of the parent class. In the constructor of a subclass, you can use the statement super (parameter list) to call the constructor of the parent class.

Class defines attributes and methods, and describes the abstraction of a certain type of thing

Logically speaking, single inheritance makes it clear that a subclass should be among the things represented by its parent class A more specific category should not be both this kind of thing and that kind of thing
And from a practical point of view, single inheritance is easy to read, easy to maintain, has clear semantics, and clear logic

Problems caused by multiple inheritance. Diamond Crisis:

  A foo()
       / \
      /   \
  B foo()   C foo()
      \   /
       \ /
      D foo()
Copy after login

Suppose there is a public method fun() in class A, then class B and class C inherit class A at the same time, and class B or class C each performs fun() Override, then class D inherits both class B and class C through multiple inheritance, which leads to a diamond crisis. How to judge the corresponding method fun() when the program is running?

class A{
    public void foo(){
    }
}
class B extends A{
    public void foo(){
    }
}
class C extends A{
    public void foo(){
    }
}
//Not allowed here.
public class D extends B, C{
    public void foo(){
    }
}
Copy after login

(1) If there are instance variables with the same name in multiple parent classes inherited by a subclass, the subclass will have ambiguity when referencing the variable and cannot determine which parent class variable should be used.

(2) If there is the same method in multiple parent classes inherited by a subclass, and whether the method is overridden in the subclass, there will be ambiguity when calling the method, and it is impossible to determine which parent class should be called. Methods.

The above is the detailed content of The reason why java does not have multiple inheritance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!