Functional interface

Java 8 functional interface

Single Abstract Method interfaces (SAM Interfaces) is not a new concept. It means interfaces with only one single method. In java, we already have many examples of such SAM interfaces. From java 8, they will also be referred as functional interfaces as well. Java 8, enforces the rule of single responsibility by marking these interfaces with a new annotation i.e. @FunctionalInterface.

@FunctionalInterface
public interface Runnable {

public abstract void run();

}

new Thread(new Runnable() {

@Override

public void run() {
System.out.println(“howtodoinjava”);

}

}).start();


 

@FunctionalInterface

interface MyFunctionalInterface {

public int addMethod(int a, int b);

}


 

public class BeginnersBookClass {
public static void main(String args[]) {

// lambda expression

MyFunctionalInterface sum = (a, b) -> a + b;
System.out.println(“Result: “+sum.addMethod(12, 100));
}
}

%d bloggers like this: