Junior — Middle
What is the difference between a standard interface and a functional interface in programming?
sobes.tech AI
Answer from AI
The standard interface in programming is an interface that can contain multiple methods and describes a set of operations that a class or object must implement.
A functional interface is an interface with exactly one abstract method. In Java, starting from version 8, functional interfaces are widely used for lambda expressions and method references.
Main differences:
- Number of methods: A standard interface can have many methods, while a functional interface has only one abstract method.
- Usage: Functional interfaces allow the use of lambda expressions, which simplifies the code and makes it more expressive.
Example of a functional interface in Java:
@FunctionalInterface
interface Converter<F, T> {
T convert(F from);
}
// Usage with lambda
Converter<String, Integer> stringToInteger = Integer::valueOf;
int number = stringToInteger.convert("123");
Thus, a functional interface is a special case of a standard interface designed for functional programming.