Functional Interface in java 8 with easy examples




1) What are Functional interface in java 8?
Functional interface are those interfaces that can exactly have one abstract method in java 8.

2) How can we create/make interface a FunctionalInterface in java 8?
We can make interface a FunctionalInterface by using annotation @FunctionalInterface in java 8.

3) Advantage of making Functional interface in java 8?
It is very handy in ensuring that your interface has exactly defined one abstract method in java 8.
Functional interface can be used with Lambda expression, which makes code much neat, clean and easy to read. Read example 4.2 below. Also read this article for more details .


4) Program/Example of defining/creating @FunctionalInterface and using it in java 8 >

4.1) Full Program/Example of defining/creating @FunctionalInterface - And write Anonymous inner class to implement method of FunctionalInterface in java 8 >
//Define/Create @FunctionalInterface in java 8
@FunctionalInterface
interface MyInterface {
   public abstract void myMethod();
}
public class MainClass{
   public static void main(String...args) {
         
          //Write Anonymous inner class to implement method of MyInterface (FunctionalInterface)
          MyInterface myInterface = new MyInterface() {
                 @Override
                 public void myMethod() {
                       System.out.println("xx");
                 }
          };
         
          //Call myMethod()
          myInterface.myMethod();
   }
}
/* OUTPUT
xx
*/

So. in this program we created @FunctionalInterface
And then in main method We wrote Anonymous inner class to implement MyInterface (FunctionalInterface)

4.2) Full Program/Example of defining/creating @FunctionalInterface  - And write Lambda expression to implement method of FunctionalInterface in java 8 >
//Define/Create @FunctionalInterface in java 8
@FunctionalInterface
interface MyInterface {
   public abstract void myMethod();
}
public class MainClass{
   public static void main(String...args) {
         
          //Write LAMBDA EXPRESSION to implement method of MyInterface (FunctionalInterface)
          MyInterface myInterface = () -> {
                System.out.println("xx");
          };
         
          //Call myMethod()
          myInterface.myMethod();
   }
}
/* OUTPUT
xx
*/




5) If no abstract method is defined in functional interface we will face compilation error in java 8
Solution > For solving it we need to define exactly have one abstract method in interface in java 8.

What’s exact error in eclipse in java 8 ?
“Invalid '@FunctionalInterface' annotation; MyInterface is not a functional interface”


6) If more than one abstract method is defined in functional interface we will face compilation error in java 8
Solution > For solving it we need to define exactly have one abstract method in interface.



7) @FunctionalInterface can have static and default method in java 8
Example of FunctionalInterface having static and default method in java 8 >
@FunctionalInterface
interface MyInterface1 {
   abstract void myMethod();
   default void defaultMethod() {}
   static void staticMethod() {}
}



Related :

Why Static method cannot be overridden in java - Explanation with program


Labels: Java 8
Must read for you :