Home  >  Article  >  Java  >  Why doesn't Java support multiple inheritance?

Why doesn't Java support multiple inheritance?

WBOY
WBOYforward
2023-09-20 22:17:02905browse

Why doesnt Java support multiple inheritance?

In Java, a class cannot extend multiple classes. Therefore the following is illegal -

Example

public class extends Animal, Mammal{}

However, a class can implement one or more interfaces, which helps Java get rid of the impossibility of multiple inheritance.

The reason for this is to prevent ambiguity.

Consider a situation where class B extends class A and class C, and both classes A and C have the same method display().

Now the java compiler cannot decide which display method it should inherit. To prevent this, multiple inheritance is not allowed in java.

The above is the detailed content of Why doesn't Java support multiple inheritance?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete