Suppose there are two interfaces Interface1 and Interface2 where Interface2 extends Interface1.
interface Interface1 {
default void method() {
System.out.println("1");
}
// Other methods
}
interface Interface2 extends Interface1 {
@Override
default void method() {
System.out.println("2");
}
// Other methods
}
Suppose I want to create a class that implements Interface2 but I want method() to be the version in Interface1. If I write
class MyClass implements Interface1, Interface2 {
public void method() {
Interface1.super.method();
}
}
I get the compilation error:
bad type qualifier in default super call: redundant interface Interface1 is extended by Interface2
It is possible to get around this by creating a third interface:
interface Interface3 extends Interface1 {
default void method() {
Interface1.super.method();
}
}
Then:
class MyClass implements Interface1, Interface2, Interface3 {
public void method() {
Interface3.super.method();
}
}
This compiles fine, and if I instantiate a new MyClass and invoke method(), the output is 1 as expected.
So my question is, given that it is so easy to get around the restriction that you can only write InterfaceName.super.method() for the most specific interface in a chain, what is the reason for the restriction? What problems are prevented by disallowing you from writing Interface1.super.method() in the first place?