In this lesson, you will learn
Multiple inheritance refers to the ability of a class to inherit behaviors and attributes from more than one parent class.
In the below image, we can observe that Class C (sub-class) inherits from more than one superclass i.e., Class A, Class B. This is the concept of Multiple Inheritance.

Java does not support multiple inheritance directly through classes to avoid the complexity and ambiguity it can cause.
Let’s discuss the problem of why Java does not support multiple inheritance.
The primary problem with multiple inheritance arises from ambiguity and complexity in the inheritance hierarchy, especially evident in the diamond problem.
The Diamond Problem
Consider four classes A, B, C, and D arranged in a diamond pattern, where B and C inherit from A, and D inherits from both B and C. If both B and C override an inherited method from A, and D does not override it, there is an ambiguity about which version of the method D should inherit: the one from B or the one from C?

This concept is common in some programming languages but comes with its own set of complexities, such as the “Diamond Problem,” where the compiler cannot decide which parent class’s method to prioritize if both have a method with the same signature.
Note: Java does not support multiple inheritance directly through classes due to these complexities. However, Java allows multiple inheritance of behavior through interfaces.

package ch10.l6;
interface Animal {
void eat();
}
interface Mammal {
void sleep();
}
class Bat implements Animal, Mammal {
public void eat() {
System.out.println("Bat is eating");
}
public void sleep() {
System.out.println("Bat is sleeping");
}
}
public class AnimalMutipleInheritnace {
public static void main(String[] args) {
Bat bat=new Bat();
bat.eat();
bat.sleep();
}
}
Bat is eating
Bat is sleeping
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.