0
interface WithDefinitionsInter {
    default void definedMeth() {
        System.out.println("inside interface");
    }
}
class WithDefinitionsImpl implements WithDefinitionsInter {
    public void definedMeth() {
        super.definedMeth(); // compilation error here
        System.out.println("inside class");
    }
}
public class QuizDef {
    public static void main(String par[]) {
        WithDefinitionsInter withDef = new WithDefinitionsImpl();
        withDef.definedMeth();
    }
}

Can someone please explain why I am not able to call default method of parent interface using super keyword.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Manish Bansal
  • 819
  • 2
  • 8
  • 19
  • The superclass of `WithDefinitionsImpl` is not an interface. In your example, the superclass of `WithDefinitionsImpl` is `java.lang.Object` which does not declare method `definedMeth`. – Abra Aug 01 '21 at 09:29
  • 1
    As @Abra said, that's because `super` doesn't consider `WithDefinitionsInter` here, since it's the interface, but you can be explicit: `WithDefinitionsInter.super.definedMeth()` will work. The reason you need to be explicit is because multiple interfaces can provide a default method (and that can even change after compilation), so to avoid any ambiguity, this is necessary. – Joachim Sauer Aug 01 '21 at 09:34

0 Answers0