functional interface in java
A functional interface in Java is an interface that contains only one abstract method. Functional interfaces are a key concept in Java’s functional programming paradigm and are used to enable the use of lambda expressions or method references. Despite containing a single abstract method, functional interfaces may also have multiple default or static methods (introduced in Java 8) without violating the functional interface contract.
@FunctionalInterface annotation is optional but can be used to indicate that an interface is intended to be a functional interface. This annotation helps the compiler catch errors if the interface violates the rules of a functional interface.
Here are the key characteristics of a functional interface:
1. Single Abstract Method (SAM):
- A functional interface must have exactly one abstract method. This method is often referred to as the Single Abstract Method (SAM).
- The presence of multiple abstract methods in an interface would violate the functional interface contract.
2. Multiple Default or Static Methods:
- In addition to the single abstract method, a functional interface can have multiple default or static methods without breaking the functional interface contract.
3. Lambda Expressions and Method References:
- Functional interfaces are designed to be used with lambda expressions or method references, providing a concise way to represent instances of a single-method interface.
@FunctionalInterface Annotation (Optional):
@FunctionalInterfaceannotation is optional but can be used to explicitly indicate that an interface is intended to be a functional interface.
- If an interface is annotated with
@FunctionalInterfaceand doesn’t satisfy the conditions (e.g., has more than one abstract method), the compiler generates an error.
Here’s an example of a functional interface:
In this example,
MyFunctionalInterface is a functional interface because it has a single abstract method (
myMethod) along with additional default and static methods.
Functional interfaces play a crucial role in supporting functional programming constructs in Java, allowing the use of lambda expressions and providing a more expressive and concise way to work with certain types of interfaces.
Some Built-in Java Functional Interfaces
Certainly! Here are brief definitions and examples for four kinds of functional interfaces in Java:
1. Function: Represents a function that takes one argument and produces a result.
2. Consumer: Represents an operation that takes a single input argument and returns no result.
3. Predicate: Represents a predicate (boolean-valued function) of one argument.
4. Supplier: Represents a supplier of results, producing a result without taking any input.
These examples showcase the use of each functional interface by creating instances through lambda expressions and applying or testing them accordingly. These functional interfaces are part of the
java.util.function package introduced in Java 8 and provide a convenient way to work with functional programming constructs.