Default & Static Interface Methods in Java
Since Java 8, an interface can include default methods — declared with the default keyword and a real method body right inside the interface — giving implementing classes a ready-made implementation they can use as-is or override if they need different behavior. This solved a genuine backward-compatibility problem: before default methods existed, adding any new method to an already-published interface would break every existing class that implemented it, since each would suddenly be missing a required implementation. A default method can be added without breaking anything, since implementing classes simply inherit it automatically.
An interface can also declare static methods, which belong to the interface itself rather than to any implementing class, called through the interface's own name, like Comparator.naturalOrder(), and cannot be overridden by implementing classes at all.
Declaring a default method
The default keyword plus a real method body, written directly inside the interface itself.
Implementing classes inherit it automatically
With no obligation to override it, though they still can if different behavior is needed.
Why default methods exist
Adding an ordinary abstract method to an already-published interface would break every existing implementing class; a default method doesn't.
Overriding a default method
An implementing class can still supply its own version, which then takes priority over the interface's default one for that class.
Static interface methods
Belong to the interface itself, called through its name, and can never be overridden by an implementing class.
interface Greeter { default void greet() { System.out.println("Hello from the default method"); }} public class CustomGreeter implements Greeter { public static void main(String[] args) { CustomGreeter c = new CustomGreeter(); c.greet(); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.