Functional Interfaces

Functional interface are interfaces with only one abstract method in it.
@FunctionalInterface annotation is added so that we can mark an interface as functional interface.

It is not mandatory to add this annotation to our interface but it is best if we do because if some one creates a lambda expression out of the interface and later some one adds method to that interface then those lambda expression will stop working.

Note : Concrete methods in a interface does not count, i.e there is 1 abstract method and many concrete methods in a interface; it is still a Functional Interface

@FunctionalInterface
public interface MathDemo {
	public int add(int x,int y);
}
  • Java 8 comes with new package java.util.function which provides 43 interfaces which will help you create Functional interfaces providing target types for lambda expressions and method references.
  • This some of the functional interfaces provided by java 8 so that we don’t create just them manually every time we use lamdas

All the 43 interfaces can be divided into 4 categories : java doc for methods

  1. Supplier (Parameter as object and Returns the same type of Object)
  2. Consumer/Bi-Consumer (Parameter as object but does not return anything)
  3. Predicate/Bi-Predicate (Parameter as object but returns boolean)
  4. Function/Bi-Function (Parameter as object and returns a different object)

Leave a Comment