Java8 - Multiple inheritance and conflict resolution

Category : Java | Sub Category : Java8 Features | By Prasad Bonam Last updated: 2023-11-13 04:57:17 Viewed : 236



In Java, interfaces support multiple inheritance, meaning a class can implement multiple interfaces. However, conflicts can arise when a class implements two or more interfaces that declare methods with the same signature (name and parameter types). This is known as a "diamond problem" or "interface method conflict." Java provides rules and mechanisms to resolve such conflicts:

Interface Method Conflict:

Consider the following example:

java
interface A { void method(); } interface B extends A { // No method declaration here } interface C extends A { // No method declaration here } class D implements B, C { // Class D must provide an implementation for method() @Override public void method() { System.out.println("Implementation in class D"); } }

In this example, both interfaces B and C extend interface A, and class D implements both B and C. The common method method() is declared in A, and class D must provide an implementation to resolve the conflict.

Resolution Strategies:

  1. Class Overrides the Method:

    • If the class directly provides an implementation for the conflicting method, that implementation is used.
    java
    class D implements B, C { // Class D provides its own implementation @Override public void method() { System.out.println("Implementation in class D"); } }
  2. Interface Provides a Default Method:

    • If the conflicting method is declared as a default method in one of the interfaces, the default method is used unless the class provides its own implementation.
    java
    interface B extends A { // Default method provided in interface B default void method() { System.out.println("Default implementation in interface B"); } } interface C extends A { // Default method provided in interface C default void method() { System.out.println("Default implementation in interface C"); } } class D implements B, C { // Class D does not provide its own implementation, uses default from interface B }
    } interface C extends A { // Default method provided in interface C default void method() { System.out.println("Default implementation in interface C"); } } class D implements B, C { // Class D does not provide its own implementation, uses default from interface B }
  3. Class Explicitly Chooses Interface Method:

    • If the class explicitly wants to use the method from a specific interface, it can do so by invoking the method on that interface.
    java
    class D implements B, C { // Class D explicitly chooses the method from interface B @Override public void method() { B.super.method(); } }

    Here, B.super.method() explicitly calls the method() from interface B.

Priority of Resolution:

  1. Class Hierarchy:

    • If the class hierarchy provides an implementation, that takes precedence.
  2. Interface Default Methods:

    • If no class in the hierarchy provides an implementation, the default methods in interfaces are considered.
  3. Explicit Interface Invocation:

    • If a class explicitly invokes a method from a specific interface, that method is used.

Example:

java
public interface A { default void method() { System.out.println("Default implementation in interface A"); } } public interface B extends A { // No method declaration here } public interface C extends A { // No method declaration here } public class D implements B, C { // No implementation here } class Main { public static void main(String[] args) { D d = new D(); d.method(); // Output: Default implementation in interface A } }

In this example, interface A provides a default method method(). Interfaces B and C extend A. Class D implements both B and C. Since class D does not provide its own implementation, it inherits the default implementation from interface A.

Search
Related Articles

Leave a Comment: