100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What are Default Methods in Interfaces?

Learn default methods in Java interfaces — why they exist, conflict resolution and override rules — with examples and interview Q&A.

mediumQ118 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

A default method is a method in a Java interface that provides a concrete implementation using the default keyword, allowing implementing classes to inherit ready-made behavior instead of being forced to override it.

Before Java 8, interfaces could only declare abstract method signatures, so adding a new method to an existing interface would break every implementing class. Default methods solve this by letting an interface ship a working body, so existing implementations keep compiling and only need to override the method if they want different behavior. A class implementing multiple interfaces that define the same default method must explicitly override it to resolve the ambiguity, since Java has no automatic priority rule between unrelated interfaces. Default methods are called on an instance like any other method, and a class’s own implementation always takes precedence over an inherited default.

  • Enables interface evolution without breaking existing implementers
  • Provides shared default behavior across many implementing classes
  • Reduces duplicated boilerplate for common operations
  • Underpins many java.util.function and Collection API enhancements

AI Mentor Explanation

When a cricket board updates the rulebook to add a new mandatory review-signal gesture, it also publishes a standard default gesture every umpire can use immediately, so existing umpires don’t need retraining before the rule takes effect. An umpire who wants a personal variant can still adopt their own gesture instead. That published fallback gesture is a default method: the interface (rulebook) ships ready-made behavior, so implementers keep working without being forced to redefine it themselves.

Step-by-Step Explanation

  1. Step 1

    Declare with the default keyword

    Write `default returnType methodName(...) { ... }` inside the interface with a real body.

  2. Step 2

    Implementing classes inherit it automatically

    Any class implementing the interface gets the default behavior without writing it.

  3. Step 3

    Override when needed

    A class can provide its own implementation, which takes precedence over the interface default.

  4. Step 4

    Resolve multi-interface conflicts explicitly

    If two interfaces define the same default method, the implementing class must override it to disambiguate.

What Interviewer Expects

  • Correct motivation: interface evolution without breaking implementers
  • Awareness of the diamond-style conflict when two interfaces share a default method
  • Knowledge that a class’s own method always wins over an inherited default
  • A concrete example, ideally referencing java.util.Collection or Comparator

Common Mistakes

  • Thinking default methods make interfaces the same as abstract classes (they still can’t hold instance state)
  • Assuming Java auto-resolves conflicting default methods from multiple interfaces
  • Believing default methods can be private prior to Java 9 (private interface methods came later)
  • Forgetting that InterfaceName.super.method() is required to explicitly call a specific interface’s default

Best Answer (HR Friendly)

A default method lets an interface provide an actual implementation, not just a signature, so classes that already implement that interface don’t break when a new method is added. It was introduced so library designers could evolve interfaces like Collection without forcing every existing implementation to change.

Code Example

Default method resolving interface evolution
interface Vehicle {
    void drive();

    // Added later without breaking existing implementers
    default void honk() {
        System.out.println("Beep beep!");
    }
}

class Car implements Vehicle {
    public void drive() { System.out.println("Car driving"); }
    // honk() inherited automatically, no changes needed
}

class SportsCar implements Vehicle {
    public void drive() { System.out.println("SportsCar driving fast"); }
    @Override
    public void honk() { System.out.println("VROOM-HONK!"); } // custom override
}

Follow-up Questions

  • What happens if a class implements two interfaces with the same default method?
  • Can a default method access private interface state?
  • How do default methods differ from abstract class methods?
  • Why were default methods introduced in Java 8?

MCQ Practice

1. A default method in an interface is declared using which keyword?

The default keyword marks an interface method that provides a concrete implementation.

2. If two implemented interfaces define the same default method, what must the implementing class do?

Java has no automatic priority between unrelated interfaces, so the class must override to disambiguate.

3. What was the main motivation for introducing default methods in Java 8?

Default methods let library authors add methods to interfaces without breaking existing implementing classes.

Flash Cards

Default method in one line?An interface method with a concrete body, declared using the default keyword.

Why introduced?To let interfaces evolve without breaking existing implementing classes.

Conflict rule?If two interfaces share a default method, the implementing class must override it explicitly.

Precedence rule?A class’s own method implementation always wins over an inherited default.

1 / 4

Continue Learning