Search code examples
javajava-8functional-interface

Functional Interface vs Interfaces with single methods like Runnable


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);
}

Solution

  • 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.