Functional Interface in Java – Complete Blog (From Scratch)
Introduction
Java 8 introduced a very important concept called Functional Interface. Functional interfaces are mainly used with Lambda Expressions to write shorter and cleaner code. They are widely used in modern Java, especially in Stream API, Collections, and multithreading.
In this blog, we will learn:
What is a Functional Interface
Why we use it
How to create it
Different ways to implement it
Lambda Expression
Real-life examples
Interview questions
What is a Functional Interface?
A Functional Interface is an interface that contains only one abstract method but it can have multiple default and static methods.
Definition
A Functional Interface is an interface that contains exactly one abstract method and can be implemented using a lambda expression.
Why Do We Use Functional Interface?
Before Java 8, we had to write a lot of code using anonymous classes. After Java 8, lambda expressions made the code short and easy, and lambda works only with functional interfaces.
So the main purpose of a functional interface is:
To use Lambda Expression
To reduce code
To improve readability
To support functional programming in Java
How to Create a Functional Interface
There are two ways:
Without annotation
With
@FunctionalInterfaceannotation
Example 1: Without Annotation
interface A {
void show();
}
Example 2: With Annotation
@FunctionalInterface
interface A {
void show();
}
If we add more than one abstract method, it will give an error.
@FunctionalInterface
interface A {
void show();
void display(); // Error: Not a functional interface
}
Ways to Implement Functional Interface
There are 3 ways to implement a functional interface:
1. Using Normal Class
interface A {
void show();
}
class B implements A {
public void show() {
System.out.println("Hello from Normal Class");
}
}
class Test {
public static void main(String[] args) {
A obj = new B();
obj.show();
}
}
2. Using Anonymous Class
interface A {
void show();
}
class Test {
public static void main(String[] args) {
A obj = new A() {
public void show() {
System.out.println("Hello from Anonymous Class");
}
};
obj.show();
}
}
3. Using Lambda Expression
interface A {
void show();
}
class Test {
public static void main(String[] args) {
A obj = () -> System.out.println("Hello from Lambda");
obj.show();
}
}
Lambda expression reduces code and is the most commonly used method.
Lambda Expression Syntax
(parameters) -> { body }
Examples:
() -> System.out.println("Hello");
(x) -> x * x;
(a, b) -> a + b;
Functional Interface with Parameters Example
@FunctionalInterface
interface Add {
int sum(int a, int b);
}
class Test {
public static void main(String[] args) {
Add obj = (a, b) -> a + b;
System.out.println(obj.sum(10, 20));
}
}
Output:
30
Inbuilt Functional Interfaces in Java
Java already provides many functional interfaces in the package java.util.function.
| Interface | Method | Work |
|---|---|---|
| Predicate | test() | Condition check |
| Function | apply() | Input → Output |
| Consumer | accept() | Takes input, no return |
| Supplier | get() | Returns value |
| Runnable | run() | Multithreading |
| Comparator | compare() | Sorting |
Difference: Normal Interface vs Functional Interface
| Normal Interface | Functional Interface |
|---|---|
| Multiple abstract methods | Only one abstract method |
| Cannot use lambda | Can use lambda |
| More code | Less code |
| Used before Java 8 | Used after Java 8 |
Real-Life Use Case
Functional interfaces are used in:
Sorting using Comparator
Filtering using Predicate
Stream API
Thread creation using Runnable
Event handling
Example:
Runnable r = () -> System.out.println("Thread running");
new Thread(r).start();
Interview Questions
Q1: What is a functional interface? An interface with only one abstract method.
Q2: Can a functional interface have multiple methods? Yes, but only one abstract method. It can have static and default methods.
Q3: Which annotation is used for functional interface? @FunctionalInterface
Q4: Can we use lambda without functional interface? No.
Q5: Give examples of functional interfaces. Runnable, Comparator, Predicate, Function, Consumer, Supplier.
Conclusion
Functional interfaces are one of the most important features introduced in Java 8. They help in writing short, clean, and readable code using lambda expressions. They are heavily used in modern Java development, especially in Stream API and multithreading.
Final One-Line Definition
Functional Interface = Interface with one abstract method + used with Lambda Expression.
Practice Questions
Create a functional interface to find square of a number.
Create a functional interface to check if a number is prime.
Create a functional interface to find maximum of two numbers.
Implement all using lambda expression.
If you practice these, your concept will be very strong.