Home Java 8 - Default Method
Post
Cancel

Java 8 - Default Method

Java 8 - Default Method

Default methods, also known as defender methods or virtual extension methods, are a new feature introduced in Java 8 that allows interfaces to provide default implementations for methods. This feature was added to enable interface evolution without breaking backward compatibility, making it easier to add new methods to existing interfaces without breaking existing code.

Before Java 8, interfaces in Java could only contain method declarations, and any class that implemented an interface was required to provide an implementation for all of its methods. This made it difficult to add new methods to existing interfaces without breaking the existing code. With the introduction of default methods in Java 8, interfaces can now provide default implementations for methods, which are automatically inherited by any class that implements the interface. If a class provides its own implementation for a default method, that implementation takes precedence over the default implementation provided by the interface.

Default methods are defined in an interface using the default keyword, followed by the method signature and implementation:

1
2
3
4
5
6
public interface MyInterface {
    default void myMethod() {
        System.out.println("Default implementation");
    }
}

In the above example, myMethod() is a default method with a default implementation that simply prints a message to the console.

Default methods can also be overridden by a class that implements the interface, providing its own implementation of the method:

1
2
3
4
5
6
7
public class MyClass implements MyInterface {
    @Override
    public void myMethod() {
        System.out.println("Custom implementation");
    }
}

In this example, MyClass implements MyInterface and provides its own implementation of myMethod(), which overrides the default implementation provided by the interface.

Default methods can also be used to extend existing interfaces without breaking backward compatibility:

1
2
3
4
5
6
7
8
9
10
11
12
public interface MyInterface2 {
    default void myMethod() {
        System.out.println("Default implementation");
    }
}

public interface MyExtendedInterface extends MyInterface2 {
    default void myNewMethod() {
        System.out.println("New default implementation");
    }
}

In the above example, MyExtendedInterface extends MyInterface2 and provides a new default method myNewMethod(), which is automatically inherited by any class that implements MyExtendedInterface.

Conclusion

In conclusion, default methods are a powerful feature introduced in Java 8 that allows interfaces to evolve without breaking backward compatibility. They provide a way to add new methods to existing interfaces, provide default implementations for methods, and extend existing interfaces, making it easier to write reusable and flexible code. However, as with any feature, it’s important to use default methods judiciously and carefully, to avoid creating overly complex and difficult-to-maintain code.

This post is licensed under CC BY 4.0 by the author.