Junior — Middle
What is the reason for having only one abstract method in a functional interface?
sobes.tech AI
Answer from AI
A functional interface in Java contains exactly one abstract method to ensure a clear representation of a functional type. This allows the use of lambda expressions and method references, which can be automatically converted into instances of such interfaces. Having a single abstract method guarantees that the compiler and runtime can correctly associate the lambda expression with the interface without ambiguity.
Example of a functional interface:
@FunctionalInterface
public interface Converter<F, T> {
T convert(F from);
}
Here, the convert method is the only abstract method, enabling the use of lambda expressions for its implementation.