default
keyword.
interface MyInterface
{
// Abstract method (must be implemented by implementing classes)
void m1();
// Default method (has a body, optional to override)
default void m2()
{
System.out.println("This is a default method inside an interface.");
}
}
class MyClass implements MyInterface
{
@Override
public void m1()
{
System.out.println("Implementation of abstract method.");
}
}
public class MainApp1
{
public static void main(String[] args)
{
MyClass obj = new MyClass();
obj.m1(); // Calls implemented abstract method
obj.m2(); // Calls interface default
}
}
Implementation of abstract method. This is a default method inside an interface.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.