Sobes.tech
Middle

How to call the default method from an interface in a class that implements this interface?

sobes.tech AI

Answer from AI

Using the syntax InterfaceName.super.methodName().

interface MyInterface {
    default void defaultMethod() {
        System.out.println("This is a default method from the interface.");
    }
}

class MyClass implements MyInterface {
    @Override
    public void defaultMethod() {
        // Call the default method from the interface
        MyInterface.super.defaultMethod();
        System.out.println("This is a method from the implementing class.");
    }

    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.defaultMethod();
    }
}