Default and Static Interface Methods in Java 8
Since Java 8, an interface can contain method bodies. A default method provides an implementation that classes inherit unless they override it, and a static method is a helper that belongs to the interface itself.
This tutorial explains why the feature exists, how the inheritance rules work, and how to resolve a conflict when two interfaces provide the same default method.
1. Why Were Default Methods Added?
Adding a new abstract method to a published interface breaks every class that implements it. Java 8 needed to add methods such as forEach, stream and removeIf to Collection and Iterable without breaking millions of existing classes. Default methods made that interface evolution possible.
Static methods let utility code live next to the interface it serves, such as Comparator.comparing, instead of in a separate Collections-style class.
2. Syntax
interface Notifier {
void send(String message); // abstract
default void sendAll(String... messages) { // inherited by implementations
for (String m : messages) send(m);
}
static Notifier console(String channel) { // called as Notifier.console(...)
return m -> System.out.println(channel + ": " + m);
}
}
- Default methods are implicitly
publicand can be overridden. - Static interface methods are not inherited: call them through the interface name.
- Interfaces still cannot have instance fields; a default method can only use other methods of the interface.
3. Practical Examples
Example 1: Adding behaviour without breaking implementations
public class DefaultMethods {
interface Notifier {
void send(String message); // abstract
// default method: new behaviour without breaking existing classes
default void sendAll(String... messages) {
for (String m : messages) {
send(m);
}
}
// static method: a helper that belongs to the interface itself
static Notifier console(String channel) {
return m -> System.out.println("[" + channel + "] " + m);
}
}
// An existing class written before sendAll() was added still compiles
static class SmsNotifier implements Notifier {
@Override
public void send(String message) {
System.out.println("SMS: " + message);
}
}
// A class may override a default method
static class QuietNotifier implements Notifier {
@Override
public void send(String message) {
System.out.println("quiet: " + message);
}
@Override
public void sendAll(String... messages) {
System.out.println("quiet: " + messages.length + " messages batched");
}
}
public static void main(String[] args) {
new SmsNotifier().sendAll("OTP 4821", "Order shipped");
new QuietNotifier().sendAll("a", "b", "c");
Notifier.console("email").send("Welcome!");
}
}
SMS: OTP 4821 SMS: Order shipped quiet: 3 messages batched [email] Welcome!
SmsNotifier gets sendAll for free, QuietNotifier overrides it with a batched version, and Notifier.console is a static factory.
Example 2: Resolving the diamond conflict
If a class implements two interfaces with the same default method, Java does not guess. The class must override the method, and can call either version with InterfaceName.super.method().
public class DiamondConflict {
interface Camera {
default String describe() {
return "camera";
}
}
interface Phone {
default String describe() {
return "phone";
}
}
// Two interfaces provide the same default method: the class must override it,
// and can call a specific one with InterfaceName.super.method()
static class SmartPhone implements Camera, Phone {
@Override
public String describe() {
return Phone.super.describe() + " with " + Camera.super.describe();
}
}
public static void main(String[] args) {
System.out.println(new SmartPhone().describe());
}
}
phone with camera
4. Method Resolution Rules
5. Important Points and Best Practices
- Use default methods to evolve interfaces and to provide convenient combinations of abstract methods.
- Do not use them to simulate multiple inheritance of state; interfaces have no instance fields.
- A default method cannot override
equals,hashCodeortoStringfromObject. - Java 9 later added
privateinterface methods to share code between default methods.
6. Summary
- Java 8 allows default and static methods in interfaces.
- Default methods let libraries add methods without breaking implementing classes.
- Static interface methods are called through the interface name and are not inherited.
- Conflicting defaults must be resolved by overriding, optionally using
X.super.method().
Related Tutorials
7. References
Frequently Asked Questions (FAQ)
Q1. Why were default methods introduced in Java 8?
To let interfaces gain new methods, such as forEach and stream on collections, without breaking existing classes that implement them.
Q2. Can a class override a default method?
Yes. A class can override it like any other inherited method, and can still call the interface version with InterfaceName.super.method().
Q3. Are static interface methods inherited by implementing classes?
No. They must be called through the interface name, for example Comparator.naturalOrder().
Q4. What happens if two interfaces have the same default method?
The implementing class does not compile until it overrides the method. Inside the override it can delegate to one of them with A.super.method().