Last updated on February 29th, 2024
This feature is introduced in Java 8. An Interface which contains a single abstract method and any number of default and static methods. Invoking the lambda expression we need a Functional Interface is compulsory. Java Programming Language is an OOP (Object Oriented Programming) based programming language. But from Java 8 onwards we can use the benefits of OOP along with Functional Programming with the help of Lambda and Functional Interface.
Some of the Predefined Functional Interfaces are:
- Runnable —>It contains a single abstract method i.e., run().
- Callable —> It contains a single abstract method i.e., call().
- Comparable —> It contains a single abstract method i.e., compareTo().
- ActionListener —> It contains a single abstract method i.e., actionPerformed().
interface Test {
public void m1();
default void m3 {
}
public static void m3() {
}
}
It is a Functional Interface.
interface Test {
public void m1();
public void m2();
}
It is not a Functional Interface.
interface Test {
default void m3 {
}
public static void m3() {
}
}
It is not a Functional Interface.
@FuctionalInterface
interface Test {
public void m1();
default void m3 {
}
public static void m3() {
}
}
It is also a Functional Interface and the “@FuctionalInterface” annotation is used for checking if the rules of the Functional Interface are satisfied or not.
@FuctionalInterface
interface Test {
public void m1();
}
@FuctionalInterface
interface Demo extends Test {
}
It is a Functional Interface.
Example:
@FunctionalInterface
interface A{
public void m1();
}
class Demo implements A{
@Override
public void m1() {
System.out.println("Hello Functional Interface without Lambda Expression");
}
}
public class Test {
public static void main(String[] args) {
A a=new Demo();
a.m1();
}
}
Output:
Hello Functional Interface without Lambda Expression
- In this above example we have Functional Interface i.e., “A”. Because the “A” interface is having SAM(Single Abstract Method) and is also annotated with the “@FunctionalInterface” annotation. This annotation is optional to use for making a Functional Interface.
- In this example we are implementing that Functional Interface with an extra class i.e., the “Demo” class. This will extra .class file for this program.
- In this example the length code of the program is lengthy as compared to the Lambda expression implementing this Functional Interface.
- In this example is generated the three “.class” files are:
- A.class
- Demo.class
- Test.class
Example:
@FunctionalInterface
interface A{
public void m1();
}
public class Test {
public static void main(String[] args) {
//Invoking lambda expression
A a=()->System.out.println("Hello Functional Interface with Lambda Expression");
a.m1();
}
}
Output:
Hello Functional Interface with Lambda Expression
- In the above we created one Functional Interface i.e., “A”. This “A” interface is the functional interface because it contains only one SAM(Single Abstract Method) and also this interface is annotated with the “@FunctionalInterface” annotation. This annotation is optional and declared to make a Functional Interface but an Interface contains a single abstract method to treat as a Functional Interface.
- In this example we don’t need to create an extra class to implement this Functional Interface. We implemented this functional interface through Lambda Expression to enable function programming for this example.
- Here we can see and analyze how code is concise as compared to a previous example to take an extra class for implementation of this functional interface.
- In the above example is generated two “.class” files are:
- A.class
- Test. class
Conclusion:
This topic is explained What is a Functional Interface? How can we make a Functional Interface? What is the use of a “@FunctionalInterface” annotation in the functional interface? How can we invoke Lambda Expression?