00:00

Default and Static Interface Methods in Java 8

Last updated: · Standard (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

Notifier.java
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 public and 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

DefaultMethods.java
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!");
    }
}
Output
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().

DiamondConflict.java
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());
    }
}
Output
phone with camera

4. Method Resolution Rules

Which implementation wins When a class calls a method, a method declared in the class or a superclass wins first, then the most specific interface default, otherwise the class must override explicitly. 1. Class wins class or superclass method 2. Sub-interface wins most specific default 3. Still ambiguous? compile error Fix override + X.super.m()
Figure 1: Classes always win over interfaces; among interfaces the most specific one wins; otherwise you must decide in code.

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, hashCode or toString from Object.
  • Java 9 later added private interface 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