BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ OOP and types

Java language

Default and static interface methods

An interface method marked default has a body, so implementing classes get it for free and may override it.

Learn these first

At a glance

Mental model

A contract with a standard clause already filled in: you only write your own clause if the default does not suit you.

What happens, in order
  1. 1default String greet() { return "hello " + name(); } can call the interface's other (abstract) methods
  2. 2An interface can also have static helper methods and private helper methods that default methods share
  3. 3If two interfaces give the same default method, the class must override it and may pick one with A.super.greet()
Remember
Watch out
Using default methods to hold state or lots of logicNot handling the diamond conflict of two defaultsAdding an abstract method to a public interface instead of a default and breaking implementors
Example (highlighted lines explain it)
public interface Greeter {  String name();  default String greet() { return "Hello, " + name() + "!"; }  static Greeter of(String n) { return () -> n; }}
Read the full explanation (about 1 min)

An interface method marked default has a body, so implementing classes get it for free and may override it. It let Java add methods like List.sort and Map.getOrDefault to old interfaces without breaking existing code.

A mental model

A contract with a standard clause already filled in: you only write your own clause if the default does not suit you.

How it works

default String greet() { return "hello " + name(); } can call the interface's other (abstract) methods.

An interface can also have static helper methods and private helper methods that default methods share.

If two interfaces give the same default method, the class must override it and may pick one with A.super.greet().

Going deeper

Defaults make interfaces a limited form of multiple inheritance of behaviour (never of state). Use them to evolve an API or to provide conveniences built on the abstract methods (Comparator.reversed()).

A class method always wins over an interface default, and a more specific interface wins over a general one.

Under the hood

A default method is an ordinary instance method in the interface's class file. The JVM resolves a call by searching the class hierarchy first and the interfaces second, using invokeinterface.

Common mistakes

  • Using default methods to hold state or lots of logic.
  • Not handling the diamond conflict of two defaults.
  • Adding an abstract method to a public interface instead of a default and breaking implementors.

Part of a learning path

Related concepts

Frequently asked questions

What is Default and static interface methods?

An interface method marked default has a body, so implementing classes get it for free and may override it. It let Java add methods like List.sort and Map.getOrDefault to old interfaces without breaking existing code.

What are the most common mistakes with Default and static interface methods?

Using default methods to hold state or lots of logic. Not handling the diamond conflict of two defaults. Adding an abstract method to a public interface instead of a default and breaking implementors.

How can I see Default and static interface methods in real code?

Paste your own Java into Unravel and it will point out where default and static interface methods appears, explain how it works at the detail level you choose, and draw the flow of the code around it.