"A functional interface is an interface which contains only one abstract method ". They can have only one functionality to exhibit/implement.
Runnable, Comparable etc. are some of the examples of functional interfaces. Runnable has only method "run()", Comparable has "compareTo()" method.
Functional Interface is additionally recognized as Single Abstract Method i.e. "SAM" Interfaces.
Functional interface can be written as the interface with an annotation called @FunctionalInterface.
i.e.
interface Sample {
void show();
As described functional interfaces can contain only one abstract method. However, they can include any quantity of default and static methods.
In Functional interfaces, its not mandatory to use the abstract keyword as by default, the method defined inside the interface is abstract only.
From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface.
Functional Interface Examples : -
Example 1:
Before Java 8, to create instance of interface, We had to create a class and implement these interfaces or we had to create anonymous inner class objects and implement these interfaces like below .
class SampleFunctionalInterface{
public static void main(String args[])
{
// create anonymous inner class object
new Thread(new Runnable() {
@Override public void run()
{
System.out.println("New thread created");
}
}).start();
}
}
Output: -
New thread created
Example 2:
Java 8 onwards, we can assign lambda expression to its functional interface object like this:
// Java program to demonstrate Implementation of functional interface using lambda expressions
public static void main(String args[])
{
// lambda expression to create the object
new Thread(() -> {
System.out.println("New thread created");
}).start();
}
}
Output : -
New thread created
@FunctionalInterface Annotation : -
@FunctionalInterface annotation is used to ensure that the functional interface can’t have more than one abstract method.
If we try to write more than one abstract methods, the compiler flags an ‘Unexpected @FunctionalInterface annotation’ message. However, it is not mandatory to use this annotation.
Below is the example of this :
- /* Java program to demonstrate lambda expressions to implement a user defined functional interface.*/
- @FunctionalInterface
- interface Square {
- int calculateArea(int x);
- int calculatePerimeter(int x);
- }
- class Test {
- public static void main(String args[])
- {
- int a = 5;
- // lambda expression to define the calculateArea method
- Square s = (int x) -> x * x;
- int ans = s.calculateArea(a);
- System.out.println(ans);
- }
- }
Some Built-in Java Functional Interfaces : -
After Java 8 , there are many interfaces that are converted into functional interfaces. All these interfaces are annotated with @FunctionalInterface.
These interfaces are as follows –
- Runnable –> This interface only contains the run() method.
- Comparable –> This interface only contains the compareTo() method.
- ActionListener –> This interface only contains the actionPerformed() method.
- Callable –> This interface only contains the call() method.
Java 8 has included 4 major kinds of functional interfaces as mentioned below :
- Consumer
- Predicate
- Function
- Supplier
1. Consumer
2. Predicate
3. Function
4. Supplier
- In functional interfaces, there is only one abstract method supported. If the annotation of a functional interface, i.e., @FunctionalInterface is not implemented or written with a function interface, more than one abstract method can be declared inside it. However, in this situation with more than one functions, that interface will not be called a functional interface. It is called a non-functional interface.
- There is no such need for the @FunctionalInterface annotation as it is voluntary only. This is written because it helps in checking the compiler level. Besides this, it is optional.
- An infinite number of methods (whether static or default) can be added to the functional interface. In simple words, there is no limit to a functional interface containing static and default methods.
- Overriding methods from the parent class do not violate the rules of a functional interface in Java.
- The java.util.function package contains many built-in functional interfaces in Java 8.