Java Default Methods

Introduction

Default Methods are a feature introduced in Java 8 to support backward compatibility and provide a way for interface evolution. Prior to Java 8, interfaces in Java could only declare method signatures without any implementation. As a result, when a new method was added to an interface, all the classes that implemented that interface had to be updated to provide an implementation for that method. This was a major issue, particularly when dealing with third-party libraries or legacy code.

Default Methods provide a solution to this problem by allowing interfaces to provide default implementation for methods. With default methods, the interface can evolve without breaking the existing implementations. Any class that implements the interface can choose to override the default implementation or inherit it.

Creating Java Default Methods

To create a default method in an interface, we use the default keyword before the method signature. Here’s an example:

public interface MyInterface {
    void someMethod();
    
    default void defaultMethod() {
        // default implementation
    }
}

In this example, defaultMethod() is a default method in the MyInterface interface. Note that the default keyword is used before the method signature to denote that it’s a default method.

When a class implements MyInterface, it can choose to override defaultMethod() or inherit its implementation. Here’s an example:

public class MyClass implements MyInterface {
    @Override
    public void someMethod() {
        // implementation
    }
    
    // override default implementation
    @Override
    public void defaultMethod() {
        // custom implementation
    }
}

In this example, MyClass implements MyInterface and provides an implementation for someMethod(). It also overrides the default implementation of defaultMethod() to provide a custom implementation.

Default methods can also be used to provide additional functionality to existing interfaces without breaking the existing implementations. This is particularly useful in cases where a new method is required in an existing interface. With default methods, the new method can be added without breaking any existing implementations.

Java Default Methods Example

Here is a complete code example that demonstrates the use of default methods in Java:

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

public class Car implements Vehicle {
    @Override
    public void start() {
        System.out.println("Car is starting");
    }
    
    @Override
    public void stop() {
        System.out.println("Car is stopping");
    }
}

public class Main {
    public static void main(String[] args) {
        Vehicle vehicle = new Car();
        vehicle.start(); // output: Car is starting
        vehicle.stop(); // output: Car is stopping
        
        Vehicle vehicle2 = new Vehicle() {
            @Override
            public void start() {
                System.out.println("Custom vehicle is starting");
            }
        };
        vehicle2.start(); // output: Custom vehicle is starting
        vehicle2.stop(); // output: Vehicle is stopping
    }
}

In this example, we have an interface called Vehicle that declares two methods: start() and stop(). stop() is a default method that prints “Vehicle is stopping” to the console.

We also have a class called Car that implements the Vehicle interface. Car provides an implementation for start() and overrides the default implementation of stop() to print “Car is stopping” to the console.

In the main() method, we create an instance of Car and call its start() and stop() methods. We also create an anonymous inner class that implements the Vehicle interface and overrides start(). When we call stop() on this instance, it uses the default implementation provided by the Vehicle interface.

Here’s the output of running the Main class:

Java Default Methods

As you can see, the default implementation of stop() is used when we call it on the anonymous inner class that implements Vehicle. This demonstrates how default methods can be used to provide backward compatibility and interface evolution.

Conclusion

In summary, default methods are a powerful feature introduced in Java 8 to support backward compatibility and provide a way for interface evolution. They allow interfaces to provide default implementation for methods and enable classes to choose whether to override or inherit the implementation. By using default methods, interfaces can evolve without breaking the existing implementations, which is particularly useful when dealing with third-party libraries or legacy code.

Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author