Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why Java doesn't support multiple inheritance

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "why Java does not support multiple inheritance". In daily operation, I believe many people have doubts about why Java does not support multiple inheritance. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the question "why Java does not support multiple inheritance"! Next, please follow the small series to learn together!

inheritance

Object-oriented programming languages have three essential features: encapsulation, inheritance, and polymorphism. Inheritance is considered by many to be a cornerstone of Java object-oriented programming technology.

Inheritance is when a subclass inherits the characteristics and behavior of its parent class so that its objects (instances) have the properties and methods of its parent class, or it inherits methods from its parent so that it has the same behavior as its parent class.

Java inheritance is the use of existing class definitions as a basis for the creation of new classes, new class definitions can add new data or new functionality, but can not selectively inherit the parent class.

Add, we have defined a Car class, this Car contains the tire, engine, chassis, steering wheel and other attributes, but also has walking, refueling, window and other behaviors.

And if we want to define a Bus, we want to reuse these attributes and behaviors, we can do it through inheritance.

By using inheritance, we allow a relationship to exist between the Bus class and the Car class, which we usually call the parent of Bus and the subclass of Car.

In Java, inheritance is implemented using the extends keyword.

Like Car and Bus above, when writing inheritance statements, class Bus extends Car{ } where Bus is a subclass and Car is a parent.

multiple inheritance

The relationship between Bus and Car we mentioned above is actually a single inheritance, meaning that a class inherits from only one parent class.

In software development, there is also a case of multiple inheritance (multiple inheritance), which is, as the name implies, a class that inherits from multiple parent classes at the same time.

Wikipedia gives an example of multiple inheritance:

For example, you could create a class of "mammals" that has functions such as eating, reproduction, and so on; then define a subtype of "cats" that inherits these functions from its parent class without having to rewrite the program, while adding new functions of its own, such as "chasing mice."

However, cats can also be a subclass of pets, with abilities unique to pets.

As an object-oriented language, C++ supports multiple inheritance.

But multiple inheritance has been a sensitive topic for years, with opponents saying it adds complexity and ambiguity to programs.

Java does not support multiple inheritance.

Many people know that Java does not support multiple inheritance, and it should be mentioned here that inheritance refers to this inheritance behavior using the extensions keyword.

Why does Java not support multiple inheritance?

Java founder James Gosling once answered this question, saying:

"Java doesn't support one class inheriting multiple classes, mainly because we listened to people from the C++ and Objective-C camps at the beginning of the design. Because multiple inheritance creates a lot of ambiguity. "

The ambiguity problem mentioned by Gosling is actually a diamond inheritance problem brought about by C++ because it supports multiple inheritance.

Suppose we have class B and class C, both inheriting the same class A. We also have class D, which inherits class B and class C through multiple inheritance.

In this case, because D inherits both B and C, and B and C inherit A at the same time, D will inherit two attributes and methods from A due to multiple inheritance.

In this case, when using D, ambiguity arises if you want to call a method defined in A.

Because such inheritance relationships are shaped like diamonds, this problem is figuratively called the diamond inheritance problem.

C++ introduces virtual inheritance to solve the diamond inheritance problem.

Because it supports multiple inheritance, it introduces diamond inheritance problem, and because it wants to solve diamond inheritance problem, it introduces virtual inheritance. And after analysis, it turns out that we don't really want to use multiple inheritance very often.

Therefore, in Java, it is not allowed to "implement multiple inheritance", that is, a class is not allowed to inherit multiple parent classes. Java, however, allows "declarative multiple inheritance," meaning that a class can implement multiple interfaces and an interface can inherit from multiple parent interfaces. Since interfaces allow only method declarations and no method implementations (before Java 8), this avoids the ambiguity of multiple inheritance in C++.

Java 8 supports multiple inheritance

Java does not support multiple inheritance, but it supports multiple implementations, that is, the same class can implement multiple classes at the same time.

We know that before Java 8, interfaces could not have method implementations. So if a class implements multiple interfaces at the same time, there will be no ambiguity in C++. Because all methods have no method body, the real implementation is still in the subclass.

So here's the problem.

Java 8 supports default methods, meaning that interfaces can define a method with a method body.

public interface Pet { public default void eat(){ System.out.println("Pet Is Eating"); } }

And because Java supports multiple interfaces at the same time, this is equivalent to inheriting multiple methods from multiple interfaces through implementations, which is not disguised as supporting multiple inheritance.

So how does Java solve the diamond inheritance problem? We define a mammalian interface and also define an eat method.

public interface Mammal { public default void eat(){ System.out.println("Mammal Is Eating"); } }

Then define a Cat and let it implement two interfaces separately:

public class Cat implements Pet,Mammal { }

At this point, the compiler will report an error:

error: class Cat inherits unrelated defaults for eat() from types Mammal and Pet

At this point, it is required that in the Cat class, the eat() method must be rewritten.

public class Cat implements Pet,Mammal { @Override public void eat() { System.out.println("Cat Is Eating"); } }

So you can see that Java doesn't help us solve the problem of multiple inheritance ambiguity, but leaves it up to developers to solve it themselves by rewriting methods.

At this point, the study of "why Java does not support multiple inheritance" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report