Functional Interface in Java – Complete Guide with Examples
Learn what a Functional Interface is in Java, why it is important, how to create one, and how it works with Lambda Expressions and Method References. This guide includes practical examples, output, common mistakes, best practices and interview questions.
What You'll Learn
- 1. Introduction
- 2. What is a Functional Interface?
- 3. Why Use Functional Interfaces?
- 4. Examples of Functional Interfaces
- 5. Functional Interface with Lambda
- 6. Practical Example
- 7. Output
- 8. Common Mistakes
- 9. Best Practices
- 10. Interview Questions
- 11. PDF Notes
- 12. Frequently Asked Questions
- 13. Conclusion
- 14. Related Articles
1. Introduction
A Functional Interface in Java is an interface that contains exactly one abstract method. Functional interfaces became especially important with Java 8 because they provide the target type for Lambda Expressions.
They are also widely used with Method References, the Stream API, and functional programming patterns.
2. What is a Functional Interface in Java?
A functional interface is an interface that contains exactly one abstract method. It may contain multiple default methods and static methods, but it must have only one abstract method.
The @FunctionalInterface annotation can be used to clearly communicate the intention and allow the compiler to check the interface.
@FunctionalInterface
interface MyFunction {
void execute();
}
3. Why Use Functional Interfaces?
Functional interfaces allow developers to represent behavior as a value and pass that behavior to methods. This makes them especially useful for Lambda Expressions.
Key Benefits
- Enables Lambda Expressions
- Reduces boilerplate code
- Improves readability
- Supports functional programming
- Works with Stream API
- Supports Method References
4. Examples of Functional Interfaces
Java provides several commonly used functional interfaces through the java.util.function package.
- Predicate
- Function
- Consumer
- Supplier
- UnaryOperator
- BinaryOperator
Example: Function
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
Function<String, Integer> length =
text -> text.length();
System.out.println(
length.apply("Chai With Code")
);
}
}
5. Functional Interface with Lambda Expression
Lambda Expressions provide a concise way to implement the single abstract method of a functional interface.
Example
@FunctionalInterface
interface Calculator {
int add(int a, int b);
}
public class Main {
public static void main(String[] args) {
Calculator calculator =
(a, b) -> a + b;
int result =
calculator.add(10, 20);
System.out.println(result);
}
}
6. Practical Example
Functional interfaces are frequently used while processing collections. For example, the Predicate interface can be used to filter values from a collection.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<Integer> numbers =
Arrays.asList(10, 15, 20, 25, 30);
List<Integer> evenNumbers =
numbers.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
System.out.println(evenNumbers);
}
}
7. Output
8. Common Mistakes
Common Mistakes to Avoid
- Adding more than one abstract method
- Forgetting @FunctionalInterface
- Misunderstanding SAM
- Confusing default methods with abstract methods
- Creating custom interfaces unnecessarily
- Using incompatible Lambda parameters
9. Best Practices
Recommended Practices
- Use @FunctionalInterface
- Prefer standard functional interfaces when appropriate
- Keep interfaces focused
- Use meaningful method names
- Keep Lambda expressions readable
- Avoid unnecessarily complex Lambdas
10. Functional Interface Interview Questions
11. Functional Interface PDF Notes
Read the first two pages directly below. Watch the video to unlock the remaining pages and download the complete PDF.
Remaining Pages Are Locked
Watch the Chai With Code video and return to this page to unlock the remaining PDF content.
Complete PDF Notes
Download the complete Functional Interface in Java PDF for offline reading and interview preparation.
Preparing your PDF...
12. Frequently Asked Questions
13. Conclusion
Functional Interfaces are an important feature introduced with Java 8.
They provide the foundation for Lambda Expressions, Method References and many functional programming features in modern Java.
Understanding Functional Interfaces is useful for both real-world Java development and Java developer interview preparation.