Calling a default method from an interface inside a class that implements it is straightforward, and it becomes especially important when there’s a conflict between multiple default methods.
✅ How to Call a Default Method in a Class
If you’re in a class that implements the interface, and you want to explicitly call the default implementation, use this syntax:
InterfaceName.super.methodName();
🧠 Example:
interface Greetable {
default void greet() {
System.out.println("Hello from Greetable");
}
}
class Person implements Greetable {
@Override
public void greet() {
System.out.print("Person says: ");
Greetable.super.greet(); // Calls the default method
}
}
new Person().greet(); // Output: Person says: Hello from Greetable
Continue reading →