Resolving Conflicts During Multiple Inheritance in Java
Resolving conflicts during multiple inheritance in Java occurs when a class inherits from multiple parent classes that have methods with the same name and signature. Java does not enable multiple class inheritance, although it does support multiple interface inheritance. However conflicts can arise when two or more interfaces provide a method with the same name and signature. In such cases, the class implementing these interfaces needs to provide an explicit resolution for the conflicting methods.
Three rules to resolve conflicts during multiple inheritance in Java:
Rule 1: Classes take higher precedence than interfaces
Rule 2: Derived interfaces or sub-interfaces take higher precedence than the interfaces higher-up in the inheritance hierarchy
Rule 3: In case Rule 1 and Rule 2 are not able to resolve the conflict, the implementing class has to specifically override and provide a method with the same method definition
Implementation:
Rule 1: Classes take higher precedence than interfaces:
According to this rule, when a class inherits from both a class and an interface, and they both have a method with the same name and signature the method from the class takes precedence over the method from the interface. Here, The method from the interface won't be used instead the implementation from the class will be. The rule ensures that class-level definitions have higher priority when resolving conflicts.
FileName: ConflictResolutionExample.java
import java.util.*; // Interface definition interface MyInterface { default void myMethod() { System.out.println("Interface method"); } } // Class definition class MyClass { public void myMethod() { System.out.println("Class method"); } } // Class inheriting from both class and interface class MySubClass extends MyClass implements MyInterface { // No explicit implementation of myMethod() in MySubClass } class ConflictResolutionExample { public static void main(String[] args) { // Create an instance of MySubClass MySubClass obj=new MySubClass(); // Call the myMethod() method obj.myMethod(); } }
Output:
Class method
Explanation:
we have an interface MyInterface with a default implementation of the myMethod() method. We also have a class MyClass with its own implementation of the myMethod() method. The class MySubClass inherits from both MyClass and MyInterface.
Rule 2: Derived interfaces or sub-interfaces take higher precedence than the interfaces higher-up in the inheritance hierarchy:
This rule states that when a class inherits from multiple interfaces, and those interfaces have conflicting method signatures, the method defined in a derived interface or a sub-interface takes precedence over the method defined in a higher-level interface. The method implementation from the most specific interface will be used. By giving priority to derived interfaces, Java ensures that the method defined closer to the implementing class is used, providing more specific behavior.
FileName: ConflictResolutionExample.java
import java.util.*; // Interface definition interface MyInterface { default void myMethod() { System.out.println("Interface method"); } } // Derived interface definition interface MyDerivedInterface extends MyInterface { default void myMethod() { System.out.println("Derived interface method"); } } // Class inheriting from both interfaces class MyClass implements MyDerivedInterface { // No explicit implementation of myMethod() in MyClass } class ConflictResolutionExample { public static void main(String[] args) { // Create an instance of MyClass MyClass obj=new MyClass(); // Call the myMethod() method obj.myMethod(); } }
Output:
Derived interface method
Explanation:
we have an interface MyInterface with a default implementation of the myMethod() method. We also define a derived interface MyDerivedInterface that extends MyInterface and provides its own default implementation of myMethod().
Rule 3: In case Rule 1 and Rule 2 are not able to resolve the conflict, the implementing class has to specifically override and provide a method with the same method definition:
If there is a conflict between a class and multiple interfaces, or between multiple derived interfaces, and the rules mentioned above are not sufficient to resolve the conflict the implementing class must explicitly override the conflicting method and provide its own implementation. By overriding the method, the class can resolve the ambiguity and provide the desired behavior. It ensures that the class takes full control of the method implementation and resolves any remaining conflicts.
FileName: ConflictResolutionExample.java
import java.util.*; // Interface definition interface MyInterface1 { default void myMethod() { System.out.println("Interface 1 method"); } } interface MyInterface2 { default void myMethod() { System.out.println("Interface 2 method"); } } // Class implementing conflicting interfaces class MyClass implements MyInterface1, MyInterface2 { public void myMethod() { System.out.println("Class method"); } } class { public static void main(String[] args) { // Create an instance of MyClass MyClass obj=new MyClass(); // Call the myMethod() method obj.myMethod(); } }
Output:
Class method
Explanation:
we have two interfaces, MyInterface1 and MyInterface2, both providing a default method called myMethod() with different implementations. The class MyClass implements both MyInterface1 and MyInterface2.