All topics
library
intermediate

Default & Static Methods in Interfaces

Understand why Java 8 added default methods, how they enable interface evolution, and resolution rules for conflicts.

Java 8 let interfaces carry method bodies. default methods are inherited and overridable; static ones belong to the interface.

Default method = a recipe included with a kitchen appliance. You can use it as-is or customize it. The appliance (interface) provides a starting point without forcing every chef (implementor) to write the recipe from scratch.

Key Concepts

1
    interface Logger {
      void log(String msg);                                  // abstract
2
      default void warn(String msg) { log("WARN: " + msg); } // inherited
3
      static Logger console() { return System.out::println; } // on the interface
    }
4
The reason it exists: evolving an interface without breaking anyone. Adding an abstract method to Collection would have broken every implementation ever written. default void forEach(...) broke none.
Collectiondefault void forEach(...)
5
That is how stream(), forEach, removeIf and Comparator.reversed() were all added in Java 8.
stream()forEachremoveIfComparator.reversed()
6
A functional interface may still have defaults. Only abstract methods count toward the one, which is why Comparator has reversed, thenComparing and a dozen others and remains usable as a lambda.
ComparatorreversedthenComparing
7
Conflict resolution, in order.
    1. a class wins over any interface default
    2. the more specific interface wins
    3. otherwise it is ambiguous, and you must override
8
Rule 1:
    class Base { public void greet() { print("Base"); } }
    interface Greeter { default void greet() { print("Greeter"); } }
9
    class C extends Base implements Greeter { }
    new C().greet();            // "Base"
10
Rule 2: if B extends A and both define the same default, B's version wins — the nearer one.
B extends AB
11
Rule 3 is the one that stops compiling:
12
    interface A { default void f() { } }
    interface B { default void f() { } }
13
    class C implements A, B { }         // error: inherits unrelated defaults
14
    class C implements A, B {
      public void f() { A.super.f(); }  // the fix: pick one explicitly
    }
15
A.super.f() is the syntax for naming which default you mean.
A.super.f()
16
Static interface methods are not inherited. Logger.console() works; SomeImpl.console() does not. They are a home for factory methods that used to need a separate Loggers utility class.
Logger.console()SomeImpl.console()Loggers
17
Private interface methods arrived in Java 9, for helpers shared between defaults without exposing them.
18
What the interviewer is probing.1. "Why were default methods added?" Probing: interface evolution. Stalls: "To share code." Moves up: to let Collection gain forEach and stream without breaking every existing implementation — an abstract method would have broken them all.
19
2. "Class and interface both define the same method. Which wins?" Probing: the resolution order. Stalls: "The interface, it is more specific." Moves up: the class always wins; then the more specific interface; and unrelated interfaces are ambiguous and must be overridden.
20
3. "How do you call a specific interface default from an override?" Probing: the syntax. Stalls: "super.f()." Moves up: A.super.f(), naming the interface.
21
4. "Can an interface have private methods?" Probing: what Java 9 added. Stalls: "No, everything is public." Moves up: yes, since Java 9, for helpers shared between default methods without exposing them.