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().
Example:
Here is an example of functional interface:
interface MyNumber{ double getValue(); }
NOTE:
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().
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc