What is Default Methods in Java?

Default methods are a feature introduced in Java 8, allowing the declaration of methods in interfaces, apart from abstract methods. They are also known as defender methods or virtual extension methods.

With the use of default keyword, these methods are defined within the interface and provide a default implementation. This means they can be directly used by any class implementing this interface without needing to provide an implementation for these methods.

The main advantage of default methods is that they allow the interfaces to be evolved over time without breaking the existing code.

Here’s an example of a default method in an interface:

interface MyInterface {
    void abstractMethod();

    default void defaultMethod() {
        System.out.println("This is a default method in the interface");
    }
}

In the above example, any class implementing MyInterface needs to provide an implementation for abstractMethod(), but not for defaultMethod() unless it needs to override the default implementation.

Before Java 8, we could declare only abstract methods in interfaces. It means that classes which implement the interface were obliged to provide an implementation of all methods declared in an interface. However, this was not flexible for developers, especially when they wanted to add new methods to the interfaces.

For instance, here is an interface used by multiple classes:

interface Animal {
    void eat();
}

Now, if we wanted to add a new method called run(), all classes that implement Animal would need to define this method, which could potentially introduce bugs and is quite cumbersome if we have many classes that implement the interface.

To mitigate such issues, Java 8 introduced default methods in interfaces. With default methods, we can now add new methods in the interface with a default implementation, thereby having the least impact on the classes that implement the interface.

interface Animal {
    void eat();

    default void run() {
        System.out.println("Running");
    }
}

So in the updated Animal interface, the run() method is a default method. Classes implementing Animal can choose to override this method, but they are not obliged to do so. If a class does not provide an implementation for this method, the default implementation from the interface will be used.

Here’s an example implementation of the Animal interface:

class Dog implements Animal {
    @Override
    public void eat() {
        System.out.println("Dog is eating");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();  // Output: Dog is eating
        dog.run();  // Output: Running
    }
}

As you can see, the Dog class didn’t implement the run method, but we’re still able to call dog.run() because of the default implementation in the Animal interface.

Note: In case a class implements multiple interfaces and these interfaces have default methods with identical signatures, the compiler will throw an error. The class must override the method to resolve the conflict.

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.