Functional Interface Introduction
- Java 8 introduced a new terminology called Functional Interface.
- An interface having only single abstract method is called a Functional Interface.
- Main use of Functional interface is that the implementation of the one abstract method
- can be passed as a Lamda expression.
- Functional interface provide behaviour parameterization feature.
- Java 8 has provided @FunctionalInterface, this annotation can be used on any interface
- which you want explicitly treated as functional interface.
- Then compiler will give compile time error if the annotated interface does not
- satisfy the functional interface condition.
- Lamda expression can be applied to interface having only one abstract method.
Examples of existing Functional Interfaces
@FunctionalInterface
public interface Runnable{
public abstract void run();
}
@FunctionalInterface
public interface Callable<V>{
public V call() throws Exception;
}
@FunctionalInterface
public interface Predicate<T>{
boolean test(T t);
}
User defined examples
public interface Word{
public void printDetail();
}
@FunctionalInterface
public interface Forest{
public void printAnimals();
}