A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. Functional Interface is additionally recognized as Single Abstract Method Interfaces. In short, they are also known as SAM interfaces. Functional interfaces in Java are the new feature that provides users with the approach of fundamental programming.
Advantages:
The major benefit of java 8 functional interfaces is that we can use lambda expressions to instantiate them and avoid using bulky anonymous class implementation. Java 8 Collections API has been rewritten and new Stream API is introduced that uses a lot of functional interfaces.
What is lambda expression?
Lambda Expression are the way through which we can visualize functional programming in the java object oriented world. Objects are the base of java programming language and we can never have a function without an Object, that’s why Java language provides support for using lambda expressions only with functional interfaces. Since there is only one abstract function in the functional interfaces, there is no confusion in applying the lambda expression to the method. Lambda Expressions syntax is (argument) -> (body).
Advantages of using lambda expression:
Reduced Lines of Code
Sequential and Parallel Execution Support
Passing behaviours into methods
Higher Efficiency with Laziness
How to use a functional interface with lambda expression:
From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. Java 8 provides some built-in functional interfaces and if we want to define any functional interface then we can make use of the @FunctionalInterface annotation. It will allow us to declare only a single method in the interface. Let us see below example:
Here, we define a functional interface with name FunctionalInterfaceDemo with methos add and return type int, which takes i and j as integer arguments.
Since functional interfaces have only one method, lambda expressions can easily provide the method implementation. We just need to provide method arguments and business logic. For example, we can write above implementation using lambda expression as:
Note: If we have single statement in method implementation, we don’t need curly braces also. For example above Interface anonymous class can be instantiated using lambda as follows:
Hence, lambda expressions are a means to create anonymous classes of functional interfaces easily. There are no runtime benefits of using lambda expressions though but it can save lines of code. To use lambda expressions, a new package java.util.function has been added with bunch of functional interfaces to provide target types for lambda expressions and method references.
Below are some of the built in java interfaces that are converted into functional interfaces.
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.
Note: All the above interfaces use the annotation @FunctionalInterface.
Additional points to note while using functional interfaces:
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.
If the interface is annotated with @FunctionalInterface annotation and we try to have more than one abstract method, it throws compiler error.
The java.util.function package contains many built-in functional interfaces in Java 8.
Comments