DEV Community

Marcos Gabriel de Oliveira Favaretto
Marcos Gabriel de Oliveira Favaretto

Posted on

Functional Interfaces: a simpler way to use interfaces in Java.

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:

@FunctionalInterface // Makes the compiler throws an error.
interface MyFuncionalInterface {
    public double calculate(double num1, double num2);
Enter fullscreen mode Exit fullscreen mode

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.

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() {
            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);
Enter fullscreen mode Exit fullscreen mode

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

javac; java Application 1 1
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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


That post was based on Vogella's introduction about Java, Marcelo Carvalho post on Medium, Oracle Documentation and Pankaj post on Digital Ocean.

Top comments (0)