Java Reference ยท Object-Oriented Concepts
Default and static interface methods
Learn Java default and static interface methods (since Java 8): syntax, conflict resolution, Interface.super calls, interface evolution and limits.
Before Java 8 an interface could only declare abstract methods, so adding a method to a published interface broke every class that implemented it. A default method carries a body inside the interface; implementors inherit it automatically and may override it. A static method belongs to the interface itself and is called as InterfaceName.method().
These features let the standard library grow (List.sort, Map.getOrDefault, Comparator.reversed) without breaking existing code, and they let you write interfaces that offer helpers built on top of one abstract method.
interface Greeter { String name(); // abstract default String greet() { return "Hello, " + name(); } static Greeter of(String n) { return () -> n; } // static factory }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
interface Name { default ReturnType method(params) { body } // inherited by implementors, overridable static ReturnType helper(params) { body } // called as Name.helper(...) }
Default and static interface methods are implicitly public. A private method (Java 9) can hold code shared between them (private interface methods).
Method resolution order
| Situation | Winner |
|---|---|
| A class (or superclass) declares the method | The class method; defaults are ignored |
| Several interfaces, one extends the other | The most specific interface |
| Unrelated interfaces both provide a default | Compile error until the class overrides it |
| An interface re-declares the method as abstract | The class must implement it |
Usage notes
- Static interface methods are not inherited.
Impl.nonBlank()does not compile; callValidator.nonBlank(). Implementing classes and sub-interfaces do not see them. - Defaults cannot access state. An interface has no instance fields, so a default method can only call the interface's other methods.
- Cannot override
Objectmethods. A defaultequals,hashCodeortoStringis rejected. Interface.super.method()is the only way to call a specific inherited default; plainsuper.method()refers to the superclass.- Library evolution: adding a default method is binary and source compatible for most implementors, but a clash with an existing same-named method in an implementor can still change behavior, so add defaults carefully.
- A functional interface may contain any number of defaults and statics; only abstract methods count toward the single-method rule.
- Do not use defaults as a substitute for multiple inheritance of state; if you need fields, use an abstract class or composition.