Junior — Middle
Can you describe the main features of functional interfaces in programming?
sobes.tech AI
Answer from AI
Functional interfaces in programming are interfaces with a single abstract method. They are widely used to support functional programming, especially in Java 8 and above.
Main features:
- One abstract method — allows using lambda expressions or method references for implementation.
@FunctionalInterfaceannotation — not mandatory, but recommended for explicitly indicating a functional interface and for compiler checks.- Can contain default and static methods — but only one abstract method.
Example of a functional interface and its usage:
@FunctionalInterface
interface Converter<F, T> {
T convert(F from);
}
Converter<String, Integer> stringToInteger = Integer::valueOf;
int number = stringToInteger.convert("123");
This simplifies the code, makes it more readable, and supports functional programming style.