Sobes.tech
Junior — Middle

Can default method implementations be declared inside functional interfaces in Java?

sobes.tech AI

Answer from AI

Yes, in Java starting from version 8, functional interfaces can declare methods with default implementations using the default keyword. This allows adding new methods to interfaces without breaking existing implementations. However, a functional interface must contain exactly one abstract method to remain functional.

Example:

@FunctionalInterface
public interface MyFunction {
    void apply();

    default void log() {
        System.out.println("Calling apply method");
    }
}
Can default method implementations be declared inside… - sobes.tech