![]() |
VOOZH | about |
Multiple Inheritance is an object-oriented concept where a class can inherit from more than one parent class. While powerful, it can cause ambiguity when multiple parents have the same methods.
Java does not support multiple inheritance with classes to avoid ambiguity problems like the Diamond Problem, where a class may inherit the same method from multiple parent classes.
Example 1: Multiple Inheritance with Classes gives Error in Java
Java allows a class to implement multiple interfaces, to supporting multiple inheritance safely.
Interface A display Interface B display Class C display
Explanation:
In Java 8, interfaces can have default methods with implementations. If a class implements multiple interfaces with the same default method, it must override the method or explicitly choose one using InterfaceName.super.method().
Default PI1 Default PI2 Now Executing showOfPI1() showOfPI2() Default PI1 Default PI2
Explanation:
Note: Using interfaces with default methods in Java is a way to safely solve the diamond problem, allowing a class to inherit behavior from multiple interfaces without ambiguity.