Functional Interfaces in Java
What is Functional Interface?
Java functional interface was introduced in Java 8. A functional interface is an interface that can contain only one abstract method that is called as functional interface. The functional interface allows only one abstract method, But can have any number of default methods and static methods.
The functional interface is also called as Single Abstract Method Interface. In shortly, also known as SAM interfaces.
Why need Functional Interfaces?
- To call the lambda expressions we need to use functional interfaces.
- Without functional interfaces cannot be invoked the lambda expression.
- To call the lambda expressions we need to use functional interfaces.
Functional interfaces and lambda expression concepts are strongly related. The above diagram shows that the relationship between the functional interfaces and lambda expressions.
What is @FunctinalInterface annotation ?
- @FunctionalInterface is used to represent the functional interfaces.
- @FunctionalInterface ensures that the interface must have only one abstract method but can have any number of default methods and static methods. If there is more than one abstract method, Then the compiler will throw the errors.
Examples for Functional interfaces
Get Amandi Soysa’s stories in your inbox
Join Medium for free to get updates from this writer.
01)
- This is a functional interface. Because it has only one abstract method.
02)
- This is not a functional interface. Because it has two abstract methods.
- The functional interface should have only one abstract method.
03)
- This is a functional interface. Because it is consisted with one abstract method and a default method.
04)
- This is not a functional interface. Because it has two abstract methods.
- The functional interface should have only one abstract method.
- In this example has been applied the @FunctionalInterface annotation. When using this @FunctionalInterface annotation, it is explicitly prohibited to write more than one abstract methods. Then the compiler will throw the errors.
05)
- The above example is for functional interface concept.
06)
- This example is consisted with functional interface and lambda expression concepts.
Thanks for reading this article…
References
- Functional Interfaces
2. Examples for Functional Interfaces






