Lesson 23: Interfaces with Default Methods

24 Jul 2026 1 min Swarnil Singhai

Default methods let interfaces add new behavior without breaking every class that already implements them — critical for library evolution.

Real-time example

A logging interface adds a logError default method years after release, and every existing implementer gets it for free.

interface Logger {
    void log(String msg);
    default void logError(String msg) {
        log("[ERROR] " + msg);
    }
}
class ConsoleLogger implements Logger {
    public void log(String msg) { System.out.println(msg); }
}
new ConsoleLogger().logError("Payment failed"); // [ERROR] Payment failed

What's happening

ConsoleLogger never wrote logError itself — it inherited the default implementation straight from the interface.

This is exactly how Java's own Collection interfaces (like forEach) got added in Java 8 without breaking old code.

Advertisement