In java8, we are using interface.super.
Considering the below points
- There are 2 interfaces but none of the interfaces inherit to another.
- There is implementation class which is inheriting both of those 2 interfaces.
- 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();
}
}