Junior — Middle
Is it permissible to include methods with implementation in the interface?
sobes.tech AI
Answer from AI
In Java, starting from version 8, it is allowed to include methods with implementations in interfaces — these are called default methods. They enable adding new methods to interfaces without breaking existing implementations. Such methods are declared with the default keyword and contain a body.
Example:
public interface MyInterface {
void abstractMethod();
default void defaultMethod() {
System.out.println("Default implementation");
}
}
This is useful for extending interfaces without the need to change all classes that implement them.