What is Functional Interface ?
- An interface which contains only one abstract method is called as Functional interface
- In short, it is called as Single Abstract Method i.e.; SAM
- In addition to one abstract method, we can have any number of static or default methods, another feature added in Java 1.8 version
- An optional annotation @FunctionalInterface can be annotated for Functional Interface to make sure that not more than one abstract method is declared
- If more than one abstract method declared and also annotated with @FunctionalInterface, then compiler throws error
- Note: To invoke Lambda Expression we need Functional Interface and it is required
To multiply any two Integer
@FunctionalInterfaceinterface DemoInterface { public int multiply(int i, int j);}public class TestLambdaExpression { // main method public static void main(String[] args) { // Lambda Expression DemoInterface d = (i, j) -> i*j; // how to invoke Lambda Expression -> Functional Interface System.out.println(d.multiply(7, 21)); }} |
Output:
147
No comments:
Post a Comment