Accessing Java default methods in a subinterface class or implementation

I understand that the class overrides the default method, you can access the default method as follows

interface IFoo {
    default void bar() {}
}

class MyClass implements IFoo {
    void bar() {}
    void ifoobar() {
        IFoo.super.bar();
    }
}

But what about when the interface overrides the default method? Is the parent method available in any way?

interface IFoo {
    default void bar() {}
}

interface ISubFoo extends IFoo {
    // is IFoo.bar available anywhere in here?
    default void bar {}
} 

class MyClass implements ISubFoo {
    // is IFoo.bar available anywhere in here too?

    public static void main(String[] args) {
        MyClass mc = new MyClass();
        mc.bar(); // calls ISubFoo.bar
    }
}

The words used by Java for default methods are similar to those used for classes, otherwise it will be confusing / misleading. Subinterfaces "inherits" default methods and can "overwrite" them. So it seems that IFoo.bar should be accessible somewhere.

+4
source share
1 answer

, IFoo.bar() ISubFoo IFoo.super.bar().

interface IFoo {
    default void bar() {}
}

interface ISubFoo extends IFoo {
    // is IFoo.bar available anywhere in here?
    // Yes it is
    default void bar {
        IFoo.super.bar()
    }
} 

class MyClass implements ISubFoo {
    // is IFoo.bar available anywhere in here too?
    // Not it is not

    public static void main(String[] args) {
        MyClass mc = new MyClass();
        mc.bar(); // calls ISubFoo.bar
    }
}
+1

All Articles