Better applicability to make an interface functional

Asked

Viewed 159 times

6

Starting from java 8, for an interface to become functional, it needs to have only one right method ? However we have the @Functionface annotation, which explicitly defines that this interface is functional.

But what is the main difference in using or not this annotation, if with only one method it already becomes functional ?

    @FunctionalInterface
public interface FuncionalInterface <T>{
    boolean valida(T t);
}

public interface FuncionalInterface <T>{
    boolean valida(T t);
}

When printing the result is the same either with the annotation or without, see in main:

public static void main(String[] args) {
    FuncionalInterface<String> funcionalInterface = valor -> valor.matches("[0-9]{5}-[0-9]{3}");
    System.out.println(funcionalInterface.valida("45680-000"));

}

2 answers

8


As described in documentation, this annotation serves primarily to state your intention to use it as a functional interface.

One of the aspects involves leaving this intention explicit and documented in the code, which is good practice.

Documentation that is part of the code allows for a number of facilities that do not necessarily affect system execution, but can serve as a hint to your IDE correctly completing the code, for the compiler to perform some additional operation or verification or even for the next programmer who will use its classes and will not read the system manual.

In this case, as also cited in the documentation, this annotation requires the compiler to issue errors in the following cases:

  • The type in question is not an interface
  • The interface does not meet the criteria of a functional interface

So the main difference is that if you add one more method to the two interfaces you put in the question, one will compile and the other will not.

Think of the case of an interface that is used by another system and not just by its own code. Adding one more method may not do any harm to your code, but it may break third-party code.

So, in a practical way, the least that annotation does is remind you every time you stop at the class that it’s not a good idea to put one more method there.

  • 1

    I did the test I added one more method, and the IDE error, in case you now understand the annotation prevent that if someone else picks up your code, and have a functional interface, do not add more methods to it, example if an interface is functional without the annotation, another programmer can try to make it generic, example you have an interface to a DAO if there is no note on it signaling look I am a functional interface, another guy may think I have an interface with only one method I will take this shit a Daogenerico, get it? In the case functional interface can only have default methods?

  • @Hashmap I don’t know if I understood your doubt of the last comment. Unfortunately you can’t prevent someone from using any interface as functional. However, when you create classes and interfaces, call it the API, you establish a contract with whom you will use them and it is up to whom you will use to obey the contract. If a class does not have the annotation and then the code of the person breaks because another method has been added, it is her fault for not obeying the contract, so she needs to change the code. About the default method I don’t understand. You’re talking about implementing methods on interfaces?

  • Now I understood that confusing things was worth , orbigado by the answer.

5

You don’t need to do anything special for an interface to be considered functional. The compiler already identifies this type of interface by its structure.

The annotation @FunctionalInterface servepara that the fact of it being a functional interface is not by the mere coincidence of having a single one method. To do this we use the annotation @FuncionalInterface:

Mark your interface with the annotation and trigger any other method in it, so you will see that the compiler has an error.

  • 1

    I did this test and really the IDE popio points an error.

  • Is that right you noticed the difference? I hope it helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.