Let’s learn functional interface in java.
Functional interface in java
Functional interface is one kind of an interface which has only one abstract method.
Syntax:
@FunctionalInterface interface interfacename { // one abstract method }
NOTE: @FunctionalInterface is an annotation. This annotation is optional.
Before Java 8 an interface had abstract method signature but didn’t define the method itself. It’s upto the implementation class to implement the method of interface.
But in Java 8 we can write interfaces which have implementation methods. Functional interface is one kind. This is the type of interface which can be used for declaring or defining lambda expression.
We have a new way of marking a specific interface as functional interface. This gives a indication to other developer that this is meant to be a functional interface.
Here’s functional interface example.
@FunctionalInterface interface HelloWorld { void print(); } public class FunctionalInterfaceExample { public static void main(String[] args) { HelloWorld obj = () -> System.out.println("Hello World Java"); obj.print(); } }
Output:
Hello World Java
Also read – classes and objects in java