Sobes.tech
Junior — Middle

How to implement a default method inside an interface in a programming language?

sobes.tech AI

Answer from AI

Starting from Java version 8, it is possible to implement a default method inside an interface using the default keyword. Such a method has an implementation directly in the interface and does not require overriding in classes that implement this interface.

Example:

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

public class MyClass implements MyInterface {
    // No need to override defaultMethod, it is already implemented
}

This allows adding new methods to interfaces without breaking existing implementations.

How to implement a default method inside an interface… - sobes.tech