package org.my.site;
interface BaseInterface {
default void f() {
System.out.println("default Base.f()");
}
}
interface DerivedInterface extends BaseInterface {
// default void f() {
// System.out.println("default Derived.f()");
// }
}
public class DefaultExample implements DerivedInterface {
@Override
public void f() {
DerivedInterface.super.f();
}
public static void main(String[] args) {
new DefaultExample().f();
}
}
While there is no overriding default method in DerivedInterface (so it is commented as in code snippet above) the output is:
default Base.f()
But if I uncomment and default method f() is given new code in DerivedInterface, then the line "DerivedInterface.super.f();" works differently. The output is:
default Derived.f()
Why? And can I access BaseInterface.f() from my class? I was able to do it until I redefined it...
Besides, I do not exactly understand how "DerivedInterface.super.f();" line works and why "super" (DerivedInterface.super.) works...
And is it true that I cannot refer to BaseInterface from my class? I mean that line "BaseInterface.super.f();" gives error "Cannot bypass the more specific direct supertype DerivedInterface".
In this answer it says how to call interface default method from overriding method in class (but there the hierarchy is simple: class extends interface). In my code line "BaseInterface.super.f();" gives error "Cannot bypass the more specific direct supertype DerivedInterface".
In another article (its section "Multiple inheritance?") it is all about situation when 2 different interfaces (which do not extend each other - no hierarchy!) are simultaneously implemented by one class (then I1.super.f() and I2.super.f() work from within code of overriding f() method in class). My case is different because I2 extends I1 and class implements I2 only. So I1.super.f() gives error!