search
HomeJavajavaTutorialThe reason why java does not have multiple inheritance

The reason why java does not have multiple inheritance

May 21, 2019 am 11:44 AM
javamultiple inheritance

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()

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(){
    }
}

(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!

Statement
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
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function