Pergunta

In java8, we are using interface.super.

Considering the below points

  1. There are 2 interfaces but none of the interfaces inherit to another.
  2. There is implementation class which is inheriting both of those 2 interfaces.
  3. Each interface has a default method but the name is same for both of those methods.

Now in this scenario, in the implementation class it is forcing me to override the default print() method. And in the body I need to use either

Interface_one.super.print();
or
Interface_two.super.print();

I am new to Java8, I would like to understand the strange behaviours of using the interface.super. Also the implementation of print() method forcing the statements Interface_one.super.print(); or Interface_two.super.print();

 public interface Interface_one {
    public void method1();

    default void print(){
        System.out.println("this is default print in first interface");
    }
}


public interface Interface_two {
public void method2();

    default void print(){
        System.out.println("this is default print in second interface");
    }
}

public class ImplementedClass implements Interface_one, Interface_two{

    @Override
    public void method1() {
        System.out.println("Implemented Method 1");

    }

    @Override
    public void method2() {
        print();
        System.out.println("Implemented Method 2");
    }

    @Override
    public void print() {
        Interface_one.super.print();
    }

    public static void main(String[] args) {
        new ImplementedClass().method2();
    }
}
Foi útil?

Solução

If you inherit from two interfaces A and B, then A.super.foo() calls the foo (default) method of the A interface.

When you implement two interfaces which each have default methods with the same name, it's ambiguous. You need to resolve that ambiguity by explicitly stating which of the two default methods you'd like your implementation to use. You do this by overriding the method in question and calling the "parent" interface's implementation directly. You do this with the new syntax: ParentInterfaceName.super.methodName();

Outras dicas

One more thing : there is no ambiguity when overriding method as accepted answer is saying, you have to use A.super or B.super only if you want to reuse default implementation , then you tell which print() you want to use as they are two. You can omit calling any of default methods and write completely new implementation or even make that method (and class) abstract.

Java 8 , allows interfaces to have default implementation, this functionality had been given as a privilege for those who were using abstract class and want to replace them with CODE TO INTERFACE,

The least concrete class is forced to have its own implementation in case of any ambiguity. If there is no ambiguity then it simply uses its parents behaviour and does not forces to implement.

By using the same method name here we have created ambiguity.

you can opt for any implementation like below.

@Override
    public void  print(){}

If we implement hierarchical dependency.it will work.

interface Interface_one {
    default void print() {
        System.out.println("this is default print in first interface");
    }
}

interface Interface_two extends Interface_one {
    default void print() {
        System.out.println("this is default print in second interface");
    }
}

public class MultipleInterfaces implements Interface_one, Interface_two {
    public static void main(String[] args) {
        MultipleInterfaces multipleInterfaces = new MultipleInterfaces();
        Interface_one one = new MultipleInterfaces();
        Interface_two two = new MultipleInterfaces();
        multipleInterfaces.print();
        one.print();
        two.print();
    }
}
Licenciado em: CC-BY-SA com atribuição
scroll top