Updated February 20, 2023
Definition
The Java 8 functional interface is an interface that contains only one abstract method and any count of default and static methods. These functional interfaces have only one functionality. An Abstract method is a method that doesn’t need implementation while declaring it and is mandatorily overridden by a class that executes the interface. The annotation used to implement the Java 8 functional interface is @FunctionalInterface.
What is Java 8 Functional Interface?
Java 8 has a new feature that offers users a fundamental programming approach. This feature is the ‘functional interface’, also known as the ‘Single Abstract Method’ interface, that is, the SAM interface. Java 8 includes functional interfaces, Lambda expressions, and method references. The collective impact of the preceding three is that the code is made straightforward, clean, and more readable.
Key Takeaways
- Java 8 has introduced the ‘functional interface’ aka Single Abstract Method (SAM) interface.
- The Java 8 functional interface has only one abstract method along with none or some static methods and default methods.
- Lambda expressions create an instance of a functional interface.
- The java.util.function package consists of many inbuilt functions.
Why do we need Java 8 Functional Interface?
Java 8 functional interfaces are instantiated using Lambda expressions. Here, the benefit is that you can avoid the implementation of bulky anonymous classes. The amount of code is decreased to a large extent. The code becomes more clean and readable. The second advantage is that you can leverage the Stream API sequential and parallel operations support. The code is processed in lesser time. The third benefit is that you can use lambda expressions for passing the behavior of a method. In a nutshell, the Java 8 functional interface is needed to decrease code and processing time and augment readability. Before Java 8, if you wanted to implement any functionality, it was mandatory to create a class. This involved considerable boilerplate code. This code is now reduced.
Java 8 Functional Interface
A functional interface is an interface that consists of only one Abstract method. To mark a functional interface, you can use the annotation ‘@FunctionalInterface.’ The addition of this annotation is optional, but it is a good practice to include this annotation. You can prevent the accidental addition of extra methods by including this annotation. If you use this annotation and then include more than one Abstract method, the compiler throws an error.
You can use Lambda expressions to instantiate the Java 8 functional interfaces. Due to this, you can prevent the implementation of bulky anonymous classes. This is the prime advantage of functional interfaces. Developers have rewritten the Java 8 Collections API. They have introduced the new Stream API, which makes use of a large count of functional interfaces. The java.util.function package has a plethora of inbuilt functional interfaces. Some major functional interfaces in Java 8 are Consumer, Function, Supplier, and Predicate. An example of a Java 8 functional interface is java.lang.Runnable, which comprises only one Abstract method run().
Lambda Expression
Lambda expressions create an instance of a functional interface. In the Java object-oriented world, you can visualize functional programming by leveraging Lambda expressions. The foundation of the Java programming language is an object. You cannot use a function if you do not have an object. This is the reason for the Java language to endorse the usage of Lambda expressions with functional interfaces only. Functional interfaces have only one Abstract method, due to which there is no confusion while using the Lambda expression in the method. The syntax of Lambda expressions is the following.
Syntax:
(argument) (body)
Developers have used a Lambda expression to write the anonymous Runnable functional interface, which is the following.
Code:
Runnable r1 = () -> System.out.printIn("My Runnable");
In the preceding code, the functional interface is Runnable. To create its instance, you can use a Lambda expression. The run() method does not accept any argument. So, the preceding Lambda expression lacks an argument. Because the method body has only one statement, you need not include curly brackets ({}). You can use these curly brackets if there is more than one statement in the method body.
Method
When you want to refer to a method but do not want to invoke it, you can use the method reference. On similar lines, when you want to refer to a constructor but do not want to create a new instance of the named array type or class, you can use the constructor reference.
A functional interface can have an abstract method, static methods, and default methods. You can use default methods directly in a class implementing the functional interface. You can also override and redefine the default methods. You can call static methods using the functional interface name preceding the method name. You cannot override static methods using the classes that implement the functional interface.
Primitive Type
The Primitive Function functional interface is almost similar to the Function functional interface. The difference between these two is the following. The Function functional interface accepts an argument of all data types and returns a value of all data types. But, the Primitive Function functional interface accepts only one argument of a primitive data type, such as int, double, or long. The processing speed of the Primitive Function functional interface is more than the Function functional interface. The primitive-type is converted to the wrapper-type and vice versa in the case of auto-boxing and auto-unboxing. To avoid these conversions, the following Primitive Function functional interfaces are introduced: IntFunction, LongFunction, and DoubleFunction.
Examples of Java 8 Functional Interface
In the following example, a functional interface ‘speakout’ is created. It has an Abstract method ‘line.’ The class ‘FunctionalInterfaceExample’ executes the interface ‘speakout.’ In this class, the method line accepts a string argument and prints this argument. In the main() function, a new object of the class is created. This calls the ‘line’ method.
Code:
@FunctionalInterface
interface speakout{
void line(String audio);
}
public class FunctionalInterfaceExample implements speakout{
public void line(String audio){
System.out.println(audio);
}
public static void main(String[] args) {
FunctionalInterfaceExample g1 = new FunctionalInterfaceExample();
g1.line("Hello there");
}
}
Output:
Conclusion
The Java 8 functional interface minimizes the amount of code and augments readability. It consists of only one Abstract method along with none or some Static methods and Default methods. One Abstract method has only one functionality. Java 8 has several inbuilt functional interfaces. You can create instances of a functional interface by using Lambda Expressions.
Recommended Articles
This article helps you to learn about the Java 8 Functional Interface. To know more about the same topic, you can refer to these articles.