0

I am wondering what would be a use case for functional interface other than different implementations using lambda expressions?

Here is the basic example for functional interface:

/*Basic example for Functional interface with Lambda expression*/

public class Lambda_test {

    /*
     * Functional Interface annotation will not allow to declare more than one
     * abstract method which is obvious for the concept
     */

    @FunctionalInterface
    interface NameTest {

        // One abstract method
        abstract String MyName(String name);


    }

    public static void main(String[] args) {

        NameTest nametest = (name) -> "Ashwin " + name + "!";

        System.out.println("My name is " + nametest.MyName("Savaj"));

    }

}
g00glen00b
  • 41,995
  • 13
  • 95
  • 133

1 Answers1

1

According to java specification https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html

A functional interface is an interface that has just one abstract method (aside from the methods of Object), and thus represents a single function contract.

Since default methods have an implementation, they are not abstract.

In case of @FunctionalInterface :
A compiler will produce an error message unless:

  1. The type is an interface type and not an annotation type, enum, or class.
  2. The annotated type satisfies the requirements of a functional interface.