Java functional interface

An interface with exactly one abstract method is called Functional Interface.Functional Interface annotation is added so that we can mark an interface as functional interface.

It is not mandatory to use it, but it's best practice to use it with functional interfaces to avoid addition of extra methods accidentally. If the interface is annotated with @FunctionalInterface annotation and we try to have more than one abstract method, it throws compiler error.

The major benefit of java 8 functional interfaces is that we can use lambda expressions to instantiate them and avoid using bulky anonymous class implementation.

Java 8 has defined a lot of functional interfaces in java.util.function package. Some of the useful java 8 functional interfaces are Consumer, Supplier, Function and Predicate.

java.lang.Runnable is another example of functional interface with single abstract method run().

Here is an example of functional interface:

interface MyNumber{
double getValue();

Here the method getValue() is by default abstract, and it is the only method defined by MyNumber. Hence MyNumber is a functional interface, and its function defined by getValue().

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

Silansoftware Pvt.Ltd, BBSR
Call to: 0674-2361252