Java

Default Methods

Understand default and static methods in Java interfaces.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Understand default and static methods in Java interfaces. This hands-on tutorial focuses on practical implementation of default methods concepts.

Default Methods (Java 8+)

Before Java 8, interfaces could only have abstract methods. Java 8 introduced default and static methods.

Why Default Methods?

They allow adding new methods to interfaces without breaking existing implementations.

interface Vehicle {
    void start(); // Abstract method
    
    // Default method
    default void stop() {
        System.out.println("Vehicle stopped");
    }
}

class Car implements Vehicle {
    public void start() {
        System.out.println("Car started");
    }
    // No need to implement stop(), uses default implementation
}

Overriding Default Methods

You can override a default method if needed.

class Bike implements Vehicle {
    public void start() { System.out.println("Bike started"); }
    
    @Override
    public void stop() {
        System.out.println("Bike stopped safely");
    }
}

Static Methods in Interfaces

Interfaces can now have helper static methods.

interface MathUtils {
    static int add(int a, int b) {
        return a + b;
    }
}

// Usage
int sum = MathUtils.add(5, 10);
JAVA PLAYGROUND
⏳ Loading editor…

AI Mentor

Confused about "Java default and static methods in interfaces"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 3

What is the main purpose of default methods?

Backward compatibility
Faster execution
Better security