❮ Previous Next ❯

functional interface in java

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().

❮ Previous Next ❯

Follow Us


Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Email: info@silantechnology.com
      Phone: 0674-2361252
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved