Default Method vs Static Method in Java Interfaces

1. Introduction

Java 8 introduced new capabilities to interfaces, including default methods and static methods. 

A default method in a Java interface is a method with a body that provides a default implementation. It can be overridden in classes that implement the interface. 

On the other hand, a static method is associated with the interface itself rather than any object that implements the interface and cannot be overridden by implementing classes.

2. Key Points

1. Default methods help to add new functionality to interfaces without breaking the existing implementation of the interface.

2. Static methods in interfaces cannot be overridden and are not inherited by implementing classes.

3. Default methods can be accessed by instances of implementing classes and can be overridden by these classes.

4. Static methods are meant to provide utility functions related to the interface.

3. Differences

Default Method Static Method
Introduced in Java 8 to allow the addition of new methods to interfaces without breaking the existing implementations. Also introduced in Java 8 to provide utility or helper methods within interfaces.
It can be overridden in the implementing classes. It cannot be overridden by implementing classes.
Accessed through an instance of a class implementing the interface. Accessed directly through the interface, not through an instance.
Helps in evolving interfaces in a backward-compatible way. It provides a way to define utility methods related to an interface without needing an object instance.
It must provide an implementation that can be shared across implementing classes without overriding it. Provides a static method that is not inheritable and exists only within the interface.
This is used when it's beneficial for the method to be part of every implementing class with an option for overriding. This is used when you need a general utility method that doesn't require an instance of a class for its functionality.

4. Example


// Example of an interface with a default method and a static method
interface MyInterface {
    // Default method
    default void displayDefault() {
        System.out.println("Default Method Executed");
    }

    // Static method
    static void displayStatic() {
        System.out.println("Static Method Executed");
    }
}

class MyClass implements MyInterface {
    // Overriding the default method
    public void displayDefault() {
        System.out.println("Overridden Default Method Executed");
    }
}

public class Main {
    public static void main(String[] args) {
        MyInterface myInterface = new MyClass();

        // Default method is called using object - overridden version
        myInterface.displayDefault();

        // Static method is called using interface name
        MyInterface.displayStatic();
    }
}

Output:

Overridden Default Method Executed
Static Method Executed

Explanation:

1. MyClass implements MyInterface and overrides its displayDefault method.

2. When displayDefault is called on an instance of MyClass, the overridden method is executed.

3. The static method displayStatic is called directly on the interface and executes the method defined in the interface.

5. When to use?

- Use default methods to add new methods to an interface without breaking existing implementations.

- Use static methods when you need methods related to an interface but not tied to an instance of the interface.

Comments