Functional Interfaces
Details about functional interfaces in Java.
What is Functional Interface in Java?
A functional interface is an interface that contains exactly one abstract method.
It plays a important role in implementing functional style programming.
Functional interface is usually marked with
@FunctionalInterfaceannotation. While optional, this annotation helps the compiler verify that the interface adheres to the "single abstract method" rule.Java's
java.util.functionpackage provides several built-in functional interfaces.
If we annotate an interface with @FunctionalInterface but declare more than one abstract method, the compiler will throw an error.
Example of Incorrect Usage
@FunctionalInterface
interface InvalidFunctionalInterface {
void method1();
void method2(); // ERROR: More than one abstract method
}Compiler Error
InvalidFunctionalInterface is not a functional interface
multiple non-overriding abstract methods found in interface InvalidFunctionalInterfaceSample Example
Create a custom functional interface and add sample logic in the main application to use the newly created interface
Execute the program

Functional Interface vs Normal Interface
Here's a comparison table between Functional Interface and Normal Interface -
Definition
Interfaces with a single abstract method
Interfaces with one or more abstract methods
Annotation
Often annotated with @FunctionalInterface
No specific annotation required
Number of Methods
Exactly one abstract method
One or more abstract methods
Default/Static Methods
Can have default and static methods
Can have default and static methods
Use Case
Designed for lambda expressions and method references
Used for general abstraction and polymorphism
Example
java.util.function.Predicate<T>
java.util.List, java.util.Map
Syntax with Lambda
Can be instantiated using lambda expressions
Cannot be directly instantiated using lambda expressions
Java 8 and Later
Introduced in Java 8
Existing since the initial versions of Java
Simplicity
Simplifies the creation of instances
More verbose to implement
Primary Purpose
To provide target types for lambda expressions and method references
To define a contract that classes must follow
Functional Interface Example
Normal Interface Example
Last updated