In Java 8, the concept of functional interfaces was introduced. A functional interface is an interface that has only one abstract method. It is used to enable the functional programming paradigm in Java, allowing the use of lambda expressions or method references to represent a single unit of behavior.
Here are some key points about functional interfaces in Java 8:
- Single Abstract Method (SAM) Interface: A functional interface is also known as a Single Abstract Method (SAM) interface because it contains only one abstract method. It may also contain default methods or static methods, but those do not affect its status as a functional interface.
- @FunctionalInterface Annotation: To explicitly declare an interface as a functional interface, you can use the
@FunctionalInterface
annotation. This annotation is optional but provides compile-time safety by ensuring that the interface has only one abstract method. If more than one abstract method is defined, the compiler will report an error. - Lambda Expressions and Method References: Functional interfaces are designed to be used with lambda expressions and method references. Lambda expressions provide a concise way to express behavior, and functional interfaces act as the target types for lambdas. You can use lambda expressions or method references to instantiate functional interfaces.
- Built-in Functional Interfaces: Java 8 introduced several built-in functional interfaces in the
java.util.function
package to cover common use cases. These interfaces includePredicate
,Function
,Consumer
,Supplier
, and more. Each interface represents a specific type of operation or transformation and comes with its corresponding functional method(s). - Custom Functional Interfaces: You can create your own functional interfaces by defining an interface with a single abstract method. This allows you to define behavior-specific interfaces tailored to your application’s requirements.
Functional interfaces and lambda expressions greatly simplify the implementation of functional programming concepts in Java, making code more concise, readable, and expressive. They enable the use of higher-order functions and functional constructs in Java, promoting a more functional programming style.
Here’s an example of a functional interface in Java 8:
1 2 3 4 |
@FunctionalInterface interface Calculator { int calculate(int a, int b); } |
In this example, we define a functional interface called Calculator
. It has a single abstract method named calculate
that takes two integers as parameters and returns an integer result.
Now, we can use lambda expressions or method references to instantiate the Calculator
interface and provide the implementation for the calculate
method.
Here are a couple of examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Main { public static void main(String[] args) { // Example using a lambda expression Calculator addition = (a, b) -> a + b; System.out.println("Addition: " + addition.calculate(5, 3)); // Output: 8 // Example using a method reference Calculator subtraction = Main::subtract; System.out.println("Subtraction: " + subtraction.calculate(10, 4)); // Output: 6 } public static int subtract(int a, int b) { return a - b; } } |
In this example, we create two instances of the Calculator
interface: addition
and subtraction
. The first one uses a lambda expression to define the addition operation, while the second one uses a method reference to refer to the subtract
method defined in the Main
class.
We can then invoke the calculate
method on these instances, passing the required parameters to perform the respective calculations. The output demonstrates the results of the addition and subtraction operations.
This example showcases the usage of a functional interface and how lambda expressions or method references can be used to provide the implementation for its single abstract method.