Sobes.tech
Junior — Middle

What new features were introduced in Java 8 regarding interfaces?

sobes.tech AI

Answer from AI

In Java 8, several important innovations were introduced in working with interfaces:

  • Default methods: interfaces can now contain methods with implementations, marked with the default keyword. This allows adding new methods to interfaces without the need to change all classes that implement them.

  • Static methods in interfaces: interfaces can contain static methods with implementations, which are called through the interface name.

  • Private methods: to simplify code reuse within an interface, private methods can be created (starting from Java 9, but the concept is related to the development of interfaces).

Example of a default method:

public interface MyInterface {
    void existingMethod();

    default void newDefaultMethod() {
        System.out.println("This is a default method");
    }
}