0

While going through Functional Interfaces, I am not able to understand how are they different from other interfaces with a single method, like Runnable.

We can use Runnable as we try to use other Functional interfaces. Prior to Java 8, we already could create interfaces and anonymous objects for a single piece of functionality.

For example:

@FunctionalInterface
public interface ITrade {
  public boolean check(Trade t);
}

How is this different from:

public interface ITrade {
  public boolean check(Trade t);
}
TheBakker
  • 2,852
  • 2
  • 28
  • 49
Karan Khanna
  • 1,947
  • 3
  • 21
  • 49
  • Using the annotation will guarantee that it's a valid functional interface. It's works similarly to `@Override` for methods. – Bubletan Apr 13 '18 at 15:17

1 Answers1

6

There is no difference, the docs for FunctionalInterface state:

An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface [emphasis added]

and

However, the compiler will treat any interface meeting the definition of a functional interface as a functional interface regardless of whether or not a FunctionalInterface annotation is present on the interface declaration.

So the annotation is only there to indicate that the developer intended the interface to be used as a functional interface.

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • It is a good practice to always annotate, because if some day someone add another method, with the @FunctionalInterface the compiler will throw an error and you'll be able to see it immediately – Rolintocour Dec 30 '20 at 11:13