Junior — Middle
Is it possible to implement a functional interface with two abstract methods?
sobes.tech AI
Answer from AI
In Java, a functional interface is an interface with exactly one abstract method. This is a key requirement for using such interfaces with lambda expressions and method references.
If an interface contains two or more abstract methods, it is not considered a functional interface, and attempting to use it with a lambda expression will result in a compile-time error.
Example of a functional interface:
@FunctionalInterface
public interface MyFunction {
void execute();
}
If a second abstract method is added, for example:
void anotherMethod();
then the interface will no longer be considered a functional interface.
Thus, implementing a functional interface with two abstract methods is impossible.