Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to use @FunctionalInterface annotation in Java 8

If we have Interface with only one abstract method in it, it is by default Functional Interface. Can anyone please explain what additional advantage @FunctionalInterface annotation brings?

I know that if we add @FunctionalAnnotation, it will not allow someone to add another abstract method in the interface, as it will give a compilation error, but my point is even if you don't use @FucntionalInterface annotation, then also, if someone will add another abstract method, it will break all the existing lambda expressions in the code and compiler will complain.

For example:

If I have the following Interface :

public interface User {

    Integer fetchData(Integer userId);
}

with following implementation :

public class UserImpl implements User{

    @Override
    public Integer fetchData(Integer userId) {
        return 1;
    }
}

and following usage :

public class TestFunctionalInterface {

public static void main(String[] args) {
    User user = a -> a*2;
    System.out.println("FetchedData:"+user.fetchData(2));
}

}

And now,if I try to add another method in the interface like below :

public interface User {

    Integer fetchData(Integer userId);

    Integer fetchLoginDetails(Integer userId);

}

Compiler is complaining in below code :

public class TestFunctionalInterface {

    public static void main(String[] args) {
        User user = a -> a*2;
        System.out.println("FetchedData:"+user.fetchData(2));
    }

}

at line User user = a -> a*2;

With message "The target type of this expression must be a functional interface".

like image 579
javaguy Avatar asked Jun 16 '18 22:06

javaguy


People also ask

What is the use of @FunctionalInterface annotation?

The @FunctionalInterface annotation is an informative annotation that indicates whether or not an interface type declaration is meant to be a functional interface. We can create a custom functional interface using the @FunctionalInterface annotation.

Is @FunctionalInterface annotation required?

It's not mandatory to mark the functional interface with @FunctionalInterface annotation, the compiler doesn't throw any error. But it's good practice to use @FunctionalInterface annotation to avoid the addition of extra methods accidentally.

Why do we need functional interfaces in Java 8?

A functional interface is an interface that contains only a single abstract method (a method that doesn't have a body). The main reason why we need functional interfaces is that we can use them in a lambda expression and method references. This way we reduce boilerplate code.

Why do we use lambda expression in Java?

Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a Collection .


2 Answers

The functional interface can have only ONE abstract method. If you have two abstract methods then your interface is no longer functional.

If you have one abstract method you can use lambda expressions.

If you see the @FunctionalInterface annotation you know that you shouldn't add any new methods, because it will break design.

If you add new abstract method to any java interface it will break code anyway, because you need to provide implementation to concrete classes

like image 85
marekmuratow Avatar answered Oct 22 '22 08:10

marekmuratow


A compilation error would not occur if a different module was using the interface, for example if the interface was available via a dependency. Someone using your module can safely use the function in a lambda without fearing that a later update will break their code.

like image 42
Ronan Avatar answered Oct 22 '22 08:10

Ronan