Sobes.tech
Middle

Can interfaces have methods with implementation or not?

sobes.tech AI

Answer from AI

In classic interfaces, for example in Java before version 8, methods cannot have implementations — they are only declared, and the implementation is provided in the classes that implement this interface.

Starting with Java 8, interfaces introduced methods with implementations — default methods. They allow setting a standard implementation that can be overridden in classes.

Example:

public interface MyInterface {
    void abstractMethod(); // without implementation

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

Thus, interfaces can contain both abstract methods without implementations and methods with implementations (default, static). This expands the capabilities of interfaces and simplifies API evolution.