skip to content

What are default methods in a Java interface, why were they added, and how do you declare one?

level: juniorimportance: must knowfreq 70%

answer

  1. default keyword = body in an interface
  2. Added in Java 8 for backward-compatible interface evolution
  3. forEach/stream on Collection were the driver
  4. Inherited free, overridable, dynamically dispatched
  5. No instance fields, so no per-object state

basics

~20 s

A default method is an interface method with a body, marked with the default keyword. Classes that implement the interface inherit it for free, so you can add new methods to an interface without breaking existing implementers.

solid answer

~40 s

A default method is a non-abstract instance method declared in an interface using the default keyword and given a body. Before Java 8 every interface method was abstract, so adding a method to a published interface broke every existing implementer. Default methods solved this: an implementing class automatically inherits the default implementation but may override it. The classic motivation was evolving the Collection interfaces (e.g. adding forEach and stream to Iterable/Collection) without touching the thousands of existing implementations. Default methods are still public, are inherited like normal instance methods, can call other interface methods (abstract or default), but cannot access instance state because interfaces have no fields. They are dispatched dynamically at runtime like any instance method.

code

java · 16 lines
java
interface Vehicle {
    int wheels();                  // abstract — each vehicle differs
    default String describe() {    // default — shared, overridable
        return "A vehicle with " + wheels() + " wheels";
    }
}

class Car implements Vehicle {
    public int wheels() { return 4; }
    // inherits describe() unchanged
}

class Motorbike implements Vehicle {
    public int wheels() { return 2; }
    @Override public String describe() { return "A fast motorbike"; } // overrides
}

go deeper

for a junior

Knows the default keyword gives an interface method a body that implementers inherit for free and may override.

for a middle

Explains the interface-evolution motivation (adding forEach/stream to Collection without breaking implementers) and that defaults are public instance methods, dynamically dispatched, with no instance state.

for a senior

Discusses default methods as a tool for backward-compatible API evolution and mix-in behaviour, the trade-off versus abstract classes (single vs multiple inheritance), and cautions against putting stateful/complex logic in interfaces.

for a principal

Frames default methods in terms of library/API governance: how to evolve a published contract without a breaking release, the re-abstraction and conflict-resolution rules they introduce, and the maintainability cost of overusing them as a behaviour container.

## The problem default methods solve An **interface** in Java is a contract: a list of method signatures a class promises to provide. Before Java 8, **every** method in an interface was implicitly `public abstract` — it had no body, and each implementing class had to supply one. This made interfaces *impossible to evolve*: if the JDK team added a single new method to a widely-used interface like `java.util.List`, every existing class implementing `List` (in every library on earth) would suddenly fail to compile, because it would be missing an implementation of the new method. ## What a default method is A **default method** (introduced in Java 8) is an instance method *declared inside an interface* that has a concrete body, marked with the `default` keyword: ```java interface Greeter { String name(); // abstract: implementer must supply default String greeting() { // default: body provided here return "Hello, " + name(); } } ``` Any class that `implements Greeter` only has to write `name()`. It **inherits** `greeting()` automatically. It *may* override `greeting()` if it wants different behaviour, but it is not forced to. This is **backward-compatible evolution**: the JDK could add `forEach`, `stream`, `removeIf`, etc. as default methods on existing interfaces, and old code kept compiling and running unchanged. ## Key properties - **Implicitly `public`** — like all interface members (you may write `public` but it's redundant; you cannot make it `private` while also being `default`). - **An instance method** — it has access to `this`, can call the interface's other methods (abstract or default), and is **dynamically dispatched** (the most specific override at runtime wins), just like a method inherited from a superclass. - **No access to instance fields** — interfaces cannot declare instance fields (only `public static final` constants), so a default method has no per-object state to read or write; it works purely through the other (abstract) methods of the contract. - **Inheritable and overridable** — a subclass/implementer can override it with a normal method; an interface that extends another can re-abstract it (`Type method();` with no body) or re-default it. ## Why not just use an abstract class? A class can only `extends` **one** superclass, but can `implements` **many** interfaces. Default methods let you ship reusable behaviour through interfaces *without* spending a class's single inheritance slot. This is also the foundation of the *mix-in* style (e.g. `Comparable`, functional combinators like `Predicate.and`). ## A note on intent Default methods were designed primarily for **interface evolution**, not as a general dumping ground for logic. Putting lots of stateful or complex behaviour in interfaces is usually a smell; abstract classes or composition fit better when you need shared state.

  • Can a default method be final or synchronized?
    No. Interface methods cannot be final or synchronized. A default method is meant to be overridable by implementers, so final is disallowed; synchronized is also not permitted on interface methods.
  • Can a default method access instance fields of the interface?
    No, because interfaces can't declare instance fields — only public static final constants. A default method operates only via the interface's other methods and any constants.

Like a standard-issue template clause in a contract: every signatory gets the clause automatically, but any one of them can strike it out and write their own version.

saying these in an interview costs you the question

  • Saying default methods were added so interfaces could hold state — they cannot have instance fields.
  • Claiming an implementing class is forced to override a default method — it inherits it automatically.
  • Thinking default methods are static — they are instance methods, dynamically dispatched.
  • Believing default methods can be private (a method is either default-with-body-public, or private without default).

context