Sobes.tech
Middle

Kas võib liidesedel olla meetodid koos või ilma implementatsioonita?

sobes.tech AI

Vastus AI-lt

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

Starting from Java 8, interfaces introduced methods with implementation — 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 implementation and methods with implementation (default, static). This expands the capabilities of interfaces and simplifies API evolution.