Java MCQ: What is a functional interface in Java?
Answer:
Explanation:
A functional interface in Java is an interface that contains exactly one abstract method. This concept was introduced in Java 8 to facilitate the use of lambda expressions. Functional interfaces provide the target types for lambda expressions and method references.
For example, the java.util.function.Predicate
interface is a functional interface because it has one abstract method:
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
Although functional interfaces can contain multiple default or static methods, they must have only one abstract method to qualify as a functional interface. This single abstract method serves as the lambda expression’s or method reference’s target.
Using the @FunctionalInterface
annotation is optional but recommended as it helps the compiler enforce that the interface adheres to the functional interface contract.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html