Sobes.tech
Junior — Middle

Is it possible to define method implementations directly inside an interface?

sobes.tech AI

Answer from AI

Starting from Java 8, interfaces can define methods with implementations using the default keyword. These methods are called default methods and allow adding new functionality to interfaces without breaking existing implementations.

Example:

public interface MyInterface {
    void abstractMethod(); // abstract method

    default void defaultMethod() {
        System.out.println("Default implementation");
    }
}

Additionally, with Java 9, private methods appeared in interfaces, which can be used to organize code within default methods, but they are not visible outside the interface.

Is it possible to define method implementations… - sobes.tech