Sobes.tech
Junior — Middle

How does a lambda expression interact with an interface containing a single abstract method?

sobes.tech AI

Answer from AI

In Java, lambda expressions can be used to implement interfaces with a single abstract method, called functional interfaces.

When you write a lambda expression, the compiler understands that it corresponds to the implementation of the interface's single abstract method and automatically binds the lambda to that method.

For example, an interface:

@FunctionalInterface
interface Greeting {
    void sayHello(String name);
}

Using a lambda:

Greeting greeting = (name) -> System.out.println("Hello, " + name);
greeting.sayHello("World");

Thus, lambda expressions simplify syntax and allow writing more compact and readable code, replacing anonymous classes for functional interfaces.