DEV Community

Sharath Kumar
Sharath Kumar

Posted on

What Are Default and Static Methods in Java Interfaces?

Java 8 introduced a major enhancement to interfaces by allowing default and static methods. This change made interfaces more powerful and flexible, especially for backward compatibility and functional programming.

Let’s understand these concepts in a simple and practical way.


πŸ”Ή What is a Default Method?

A default method in an interface is a method that has a body (implementation).

πŸ‘‰ It is declared using the default keyword.

πŸ’‘ Example:

interface Vehicle {
    default void start() {
        System.out.println("Vehicle is starting");
    }
}

class Car implements Vehicle {
    // No need to override start()
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Why Default Methods?

Before Java 8, interfaces could only have abstract methods. Adding a new method to an interface would break all implementing classes.

πŸ‘‰ Default methods solve this problem by providing a default implementation.

βœ… Benefits:

  • Backward compatibility
  • No need to modify existing classes
  • Code reusability

πŸ”Ή What is a Static Method in Interface?

A static method in an interface belongs to the interface itself, not to implementing classes.

πŸ‘‰ It is declared using the static keyword.

πŸ’‘ Example:

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

class Test {
    public static void main(String[] args) {
        int result = MathUtil.add(10, 20);
        System.out.println(result);
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Key Differences: Default vs Static Methods

Feature Default Method Static Method
Keyword default static
Inheritance Inherited by implementing class Not inherited
Overriding Can be overridden Cannot be overridden
Access Through object Through interface name

πŸ”Ή Important Points

βœ… 1. Default Methods Can Be Overridden

class Bike implements Vehicle {
    @Override
    public void start() {
        System.out.println("Bike is starting");
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… 2. Static Methods Cannot Be Overridden

They are always called using the interface name.

MathUtil.add(5, 10);
Enter fullscreen mode Exit fullscreen mode

βœ… 3. Multiple Inheritance Conflict

If a class implements two interfaces with the same default method, you must override it.

interface A {
    default void show() {}
}

interface B {
    default void show() {}
}

class Test implements A, B {
    public void show() {
        System.out.println("Resolved conflict");
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Real-Time Use Cases

  • Adding new features to existing APIs
  • Utility/helper methods in interfaces
  • Functional programming with streams and lambdas
  • Framework design (e.g., Java Collections API updates)

πŸ”₯ Final Thoughts

Default and static methods transformed Java interfaces from simple contracts into powerful tools. They help developers write flexible, maintainable, and backward-compatible code.


🎯 Learn More – Upgrade Your Java Skills

Join the Best Core JAVA Online Training in Hyderabad and gain hands-on experience with real-time projects, expert mentorship, and interview preparation.

Top comments (0)