6

If I have two interface with the same default method and both are implementing with a class/ See this program.

interface alpha {
  default void reset() {
    System.out.println("This is alpha version of default");
  }
}

interface beta {
  default void reset() {
    System.out.println("This is beta version of default");
  }
}

class MyClass implements alpha, beta {
  void display() {
    System.out.println("This is not default");
  }
}

class main_class {
  public static void main(String args[]) {
    MyClass ob = new MyClass();
    ob.reset();
    ob.display();
  }  
}

then what will happen? And also I am getting unrelated error with this program.

Tiny
  • 27,221
  • 105
  • 339
  • 599
Manohar Kumar
  • 605
  • 9
  • 12

2 Answers2

7

You cannot implement multiple interfaces having same signature of Java 8 default methods (without overriding explicitly in child class)

. You can solve it by implementing the method E.g.

class MyClass implements alpha, beta {
  void display() {
    System.out.println("This is not default");
  }

  @Override
  public void reset() {
    //in order to call alpha's reset
    alpha.super.reset();
    //if you want to call beta's reset 
    beta.super.reset();

  }
}
sol4me
  • 15,233
  • 5
  • 34
  • 34
1

In effect,these two methods are the same in the class that implements them.If you try to implement the two methods in Intellij for instance, you only get one method. You can't declare the two even if you want to have different signatures for both of them.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132