Skip to main content

Functional Interface in Java

 In Java, a functional interface is an interface that has only one abstract method. It is a key concept in functional programming and serves as the foundation for working with lambda expressions and method references. Java 8 introduced functional interfaces to support the new features of the Java language, such as streams and lambdas.

Functional interfaces are annotated with the `@FunctionalInterface` annotation, which is optional but recommended. This annotation ensures that the interface has only one abstract method and generates a compile-time error if multiple abstract methods are declared. Although a functional interface can have default methods and static methods, it must have only one abstract method.

The single abstract method in a functional interface represents the behavior that can be implemented by lambda expressions or method references. Lambda expressions allow you to define anonymous functions concisely. They provide a way to pass behavior as an argument to methods, making code more expressive and flexible.

Java provides several built-in functional interfaces in the `java.util.function` package. Here are some commonly used functional interfaces:

1. `Predicate<T>`: Represents a predicate (boolean-valued function) that takes an argument of type T and returns a boolean value.
2. `Function<T, R>`: Represents a function that takes an argument of type T and produces a result of type R.
3. `Consumer<T>`: Represents an operation that takes an argument of type T and returns no result.
4. `Supplier<T>`: Represents a supplier of results, providing a value of type T.

Functional interfaces allow you to use lambda expressions and method references to implement their single abstract method. Here's an example that demonstrates the usage of functional interfaces:


@FunctionalInterface
interface MyFunctionalInterface {
    void doSomething();
}

public class Main {
    public static void main(String[] args) {
        // Lambda expression implementing the single abstract method
        MyFunctionalInterface myLambda = () -> System.out.println("Doing something");
        myLambda.doSomething();
    }
}



In this example, the `MyFunctionalInterface` is a functional interface with a single abstract method `doSomething()`. We create an instance of the interface using a lambda expression, which defines the behavior of the abstract method. The lambda expression `() -> System.out.println("Doing something")` represents a function that takes no arguments and prints "Doing something".

Functional interfaces make it easier to work with functional programming concepts in Java, enabling concise and expressive code. They provide a way to represent behavior as first-class citizens, allowing developers to write more functional-style code.

Comments

Popular posts from this blog

Scrum - Transparency, Inspection , and Adaptation

What is Scrum ? Scrum is a light weight agile framework that helps people, organization and team to develop the software in iterative and incremental process. Three Pillars of Scrum Transparency - Giving visibility to the significant aspects of the process to those responsible for the outcome. Inspection - Timely checks on the progress toward a sprint goal to detect undesirable variances Adaptation - Adjusting process as soon as possible to minimize any further deviation or issues   These three pillars of the scrum is achieved through different ceremonies and artifacts of the scrum. We can map the pillars as follows Scrum Values  Scrum is built with following 5 values  Commitment - People personally commit to achieving the goals of the Scrum Team Focus - Everyone focuses on the work of the Sprint and the goals of the Scrum Team Openness - The scrum team and its stakeholders agree to be open about all the work and challenges to perform the work Respect - Scrum Team members respect