Functional Interfaces: a simpler way to use interfaces in Java.
Tags: tutorial, java, interfaces, lambda
Published: 2023-01-14 20:51:25

# Functional Interfaces

Functional Interfaces are simple Interfaces with only one abstract method. The big thing to use then is for implement its behavier without create another class for it or even need of Anonymous Classes, which can reduce the number of lines of your code and make it more easily to read.

Every Interface with has only one abstract method is a Funcional Interface. However, use the `@FunctionalInterface` Annotation at the Interface's declaration is a good practice, since that one makes the Java Compiler throws an error in case the annotated Interface has more than one abstract method or if its type isn't an `interface`.

Another indirect advantage of using that type of implementations is the support of Streams with parallel operations which can improve our software performance.

# Use example.

Let's create a simple example. Like said before, an Interface can be a Functional Interface if it has only one abstract method. With it on mind, the Interface `MyFuncionalInterface` has the following code:

```java
@FunctionalInterface // Makes the compiler throws an error.
interface MyFuncionalInterface {
    public double calculate(double num1, double num2);
}
```

To implement that `calculate` method, we can create a new class or use an Anonymous Class, like usual. However, because it is a functional interface, we can implement the method by using Lambdas.

The following snippet of code shows two implementations: using Anonymous Class and using Lambdas.

```java
class Application {
    public static void main(String args[]) {

        double num1 = Double.valueOf(args[0]);
        double num2 = Double.valueOf(args[1]);

        // Implementation with anonymous class.
        MyFuncionalInterface calcWithAnonymousClass = new MyFuncionalInterface() {
            @Override
            public double calculate(double num1, double num2) {
                return num1 + num2;
            }
        };

        // Implementation with lambda.
        MyFuncionalInterface calcWithLamba = (parameter1, parameter2) -> parameter1 + parameter2;

        double resultWithAnonymousClass = calcWithAnonymousClass.calculate(num1, num2);
        double resultWithLambda = calcWithLamba.calculate(num1, num2);

        System.out.println("The sum between " + num1 + " and " + num2 + " is:");
        System.out.println("Anonymous Class: " + resultWithAnonymousClass);
        System.out.println("Lamba: " + resultWithLambda);
    }
}
```

For run that code, we can use the following commands at the location of `Application.java` file.

```
javac Application.java; java Application 1 1
```

After run it, the result showed at terminal is:

```
The sum between 1.0 and 1.0 is:
Anonymous Class: 2.0
Lamba: 2.0
```

Notice that the implementation maded for `calcWithAnonymousClass` needed of much more lines to make the same thing as `calcWithLamba`. That is the big deal of using Functional Interfaces and Lambdas: less code and more readability.

Of course, during our daily lives, many classes cannot be implemented in a functional way for many reasons: legacy code, dependency or even lack of knowledge. That resource is very useful, but on right places.

Thank you for reading, any suggestions are valid, please, let your feedback ;).

You can check the code on [GitHub](https://github.com/MarcosGOFavaretto/vogella-estudies/blob/main/functional-interfaces/Application.java)

# References

That post was based on [Vogella's introduction about Java](https://www.vogella.com/tutorials/JavaIntroduction/article.html#javadef_functionalinterfaces), [Marcelo Carvalho post on Medium](https://medium.com/@mvalho/java-8-functional-interfaces-tornando-o-java-mais-legal-72401462d0e2), [Oracle Documentation](https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html) and [Pankaj post on Digital Ocean](https://www.digitalocean.com/community/tutorials/java-8-functional-interfaces).