What is a functional interface in Java?

Java MCQ: What is a functional interface in Java?

a) An interface with only static methods
b) An interface that contains exactly one abstract method
c) An interface with only one method
d) An interface that cannot be extended

Answer:

b) An interface that contains exactly one abstract method

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

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top