Resolving Conflicts During Multiple Inheritance in Java
Last Updated :
30 Nov, 2021
A class can implement multiple interfaces in java, but what if the implemented multiple default interfaces have default methods with the same signatures? Then in the implementing class, which of the default implementations would be invoked from the several parent interfaces.
Java 8 designers have been thinking about this conflict and have specified rules of resolution for such scenarios. Let us now look at the potential conflict scenarios and the rules of resolution developed into Java 8 to avoid them.
Conflict Resolution Rules for inherited default methods in order of precedence are as follows
- 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 of Rule 1 and Rule 2 are not able to resolve the conflict then the implementing class has to specifically override and provide a method with the same method definition.
Now let us discuss them one by one to get the internal workflow understanding of the rules.
Implementation:
Rule 1 Classes take higher precedence than interfaces
Example
Java
// Java Program to illustrate
// classes take higher precedence than interfaces
// Importing input output classes
import java.io.*;
// Interface 1
interface A {
public default void m1()
{
System.out.println("m1 method of interface m1");
}
}
// Interface 2
interface B {
public default void m1()
{
System.out.println("m1 method of interface B");
}
}
// Helper class
class D {
// Main driver method
public void m1()
{
// Print statement when m1() of classD is called
System.out.println("method of class D");
}
}
// Main class
// It is implementing both the interfaces A and B
class C extends D implements A, B {
// Main driver method
public static void main(String args[])
{
// Creating an object of this class
// in the mai() method
C c = new C();
// Calling the method over the object created
// to illustrate Classes take higher precedence
// than interfaces
c.m1();
}
}
Output:
method of class D
Output explanation:
Since Class C inherits default method m1() from interface A, interface B, and superclass C. If the m1() method is invoked in Class C then the implementation in superclass C is executed.
Rule 2 Derived interfaces or sub-interfaces take higher precedence than the interfaces higher-up in the inheritance hierarchy
Example
Java
// Java Program to illustrate Derived interfaces or
// sub-interfaces take higher precedence than the interfaces
// higher-up in the inheritance hierarchy
// Interface 1
interface A {
// Method of Interface 1
public default void m1()
{
// Execution command whenever
// interface 1 is called
System.out.println("m1 method of A");
}
}
// Interface 2
// This interface is extending above interface
interface B extends A {
// Method of Interface 1
public default void m1()
{
// Execution command whenever
// interface 2 is called
System.out.println("m1 method of B");
}
}
// Main class
// Which is implementing Interface 2
class C implements B {
// Main driver method
public static void main(String args[])
{
// Creating an object of this class
// in the main method
C c = new C();
// Calling method over class object
// created above to illustrate sub-interface
// has higher precedence
c.m1();
}
}
Output explanation:
Since interface B inherits from interface A. Both have a default method m1() with the same signature. Class C implements both interfaces A & B. When m1() method is invoked on an instance of class C then the implementation in interface B is invoked as it is the lowest child/most derived interface in the inheritance hierarchy.
Rule 3 In case Rule 1 and Rule 2 are not able to resolve the conflict then the implementing class has to specifically override and provide a method with the same method definition
Example
Java
// Java Program to in which implementing class
// has to specifically override and provide a method
// with the same method definition
// to resolve the conflice
// Importing input output classes
import java.io.*;
// Interface 1
interface A {
// m1() method of Interface 1/A
public default void m1()
{
System.out.println("m1 method of interface m1");
}
}
// Interface 2
interface B {
// m1() method of Interface 2/B
public default void m1()
{
System.out.println("m1 method of interface B");
}
}
// Main Class
// This class implements both the interfaces
class C implements A, B {
// Method 1
// m1() method of class C (This class)
public void m1()
{
// Super keyword called over m1() method
// for interface 2/B
B.super.m1();
}
// Method 2
// Main driver method
public static void main(String args[])
{
// Creating an object of this class
// in the main() method
C c = new C();
// Calling the method 'm1()'
// over the class object
// in the main method()
c.m1();
}
}
Output:
m1 method of interface B
Output explanation:
- To get the desired behavior, the implementing class can, of course, invoke the specific default method from the specific parent interface. But the class still needs to override the default method to resolve the conflict and invoke it.
- Class C inherits from the interfaces A & B in the above example, all of which have the default m1() implementations (). As all A & B interfaces are parents of C, they are at the same level of the hierarchy, and C must also implement the m1() method on its own ().
Note: Inside Class C’s implementation of print() method it should invoke the specific implementation of interface A or B. For this Java 8 has a special syntax as follows:
<super-interface-name>.super<method-name>
In this case m1() method in class C will invoke m1() method of B, its parent, like this – B.super.m1()
Similar Reads
Interface Naming Conflicts in Java
Interfaces in Java consist of abstract methods (which do not contain a body) and variables (which are public static final). Implementation of the methods of the interface is defined in classes that implement that interface. It helps Java to achieve abstraction. Naming Conflicts occur when a class i
5 min read
Multiple Inheritance in Programming
Multiple Inheritance in programming is like a family tree for your code. You have a bunch of classes, which are like different members of a family. When one class inherits from another, it's like a child inheriting traits from their parents. The child class gets all the abilities (methods) and prope
6 min read
Why Java doesn't support Multiple Inheritance?
Multiple Inheritance is a feature provided by OOPS, it helps us to create a class that can inherit the properties from more than one parent. Some of the programming languages like C++ can support multiple inheritance but Java can't support multiple inheritance. This design choice is rooted in variou
5 min read
Using final with Inheritance in Java
Prerequisite - Overriding in java, Inheritance final is a keyword in java used for restricting some functionalities. We can declare variables, methods, and classes with the final keyword. Using final with inheritance During inheritance, we must declare methods with the final keyword for which we are
4 min read
How to Implement Multiple Inheritance by Using Interfaces in Java?
Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when methods with the same signature exist in both the superclasses and subclass. On calling the method, the compiler cannot determine which class m
2 min read
Java Multiple Inheritance
Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when there exist methods with the same signature in both the superclasses and subclass. On calling the method, the compiler cannot determine which c
4 min read
Multiple Inheritance in Python
Inheritance is the mechanism to achieve the re-usability of code as one class(child class) can derive the properties of another class(parent class). It also provides transitivity ie. if class C inherits from P then all the sub-classes of C would also inherit from P. Multiple Inheritance When a class
5 min read
Favoring Composition Over Inheritance In Java With Examples
In object-oriented programming (OOP), choosing between inheritance and composition is crucial for designing flexible and maintainable code. This article explores why you should favor composition over inheritance, with simple explanations and examples.What is Inheritance?Inheritance is when a new cla
4 min read
Comparison of Inheritance in C++ and Java
The purpose of inheritance is the same in C++ and Java. Inheritance is used in both languages for reusing code and/or creating an âis-aâ relationship. The following examples will demonstrate the differences between Java and C++ that provide support for inheritance. 1) In Java, all classes inherit fr
4 min read
Delegation vs Inheritance in Java
Inheritance in Java programming is the process by which one class takes the property of another other class. i.e. the new classes, known as derived or child class, take over the attributes and behavior of the pre-existing classes, which are referred to as base classes or super or parent class.Delega
3 min read