Question about default method in interface
Posted: Thu Aug 11, 2016 8:35 am
There are two interfaces, and firstInterface contains a default method getTest(). secondInterface extends the firstInterface and re-declare the default method as abstract method getTest(). Class A implements these two interfaces and implements the abstract method getTest(); My question is if Class A implements a new version of the method that has same signature with default method in firstInterface, can I get access to the default method?. I tried but it cannot compile.
the output is:
implemented method is called
implemented method is called
Code: Select all
firstInterface.super.getTest();
Code: Select all
public interface firstInterface {
public default int getTest(){
return 5;
}
}
public interface secondInterface extends firstInterface {
public int getTest();
}
public class A implements firstInterface, secondInterface {
@Override
public int getTest() {
System.out.println("implemented method is called");
return 6;
}
public class TEST3 {
public static void main(String[] args) {
A sp = new A(5);
sp.getTest();
//firstInterface.super.getTest(); // compile error
((firstInterface)sb).getTest();
}
}
implemented method is called
implemented method is called