Functional Interfaces

Ashok Veer | May 21, 2020 | Be the first to comment!
Functional Interfaces 

A functional Interface concept introduced in java 8 with some amazing features. So let’s check it out properties and example.

Functional interface in java is an interface which contains only a single abstract method and can contain any number of default and static methods which do have an implementation.


package com.vr.java8;

@FunctionalInterface
public interface MyInterface {
    
     // signle abstarct method no body
     public String m1();
    
     //can contain any number of static & default methods
     static void m2(){
           System.out.println("Body of static method m2");
     }
    
     default void m3(){
           System.out.println("Body of default method m3");
     }
    
     public static void m4(){
           System.out.println("Body of static method m4");
     }
}


Important points to remember

1] @FunctionalInterface annotation is used for declaring functional interface but it is not mandatory to specify the annotation in functional interface.

2] It is always good practice to use @FunctionalInterface annotation in functional interface to avoid accidently adding more than 1 abstract method in interface. 

3] If we try to add more than one abstract method in functional interface which contains @FunctionalInterface  annotation then compiler immediately show  the Invalid '@FunctionalInterface' annotation; MyInterface is not a functional interface error.

Functional interface 2 abstract method compilation error - techfloaters
Java 8 Functional Interfaces and Lambda Expressions help us in writing smaller and cleaner code by removing a lot of boiler-plate code.

The major benefit of java 8 functional interfaces is that we can use lambda expressions to instantiate them. 

Below is example which shows the implementation of functional interface using lambda expression. Runnable interface is functional interface which contains only one abstract method run().

Example of Runnable interface before Java 8 using anonymous inner class.
public class FunctionInterfaceDemo {

      public static void main(String[] args) {
           
            // create anonymous inner class object
            Runnable r = new Runnable() {
                  @Override
                  public void run() {
                        System.out.println("child thread created");
                  }
            };
           
            Thread t = new Thread(r);
            t.start();
            System.out.println("main thread created");
           
      }
}
 Output
main thread created
child thread created

Examples of Runnable interface run method implementation after Java 8 using lambda expression
public class FunctionInterfaceJava8Demo {

      public static void main(String[] args) {
           
new Thread( () -> {System.out.println("child thread    created");}).start();

            System.out.println("main thread created");
      }
}
Output
main thread created
child thread created

So form above example we checked that size of code and implementation is much simpler using lambda expression of java 8.

In Java collections API there is stream API is introduced in java 8 which is most useful in the functional interface. So in java 8 new special package is introduced java.util.function package. In that package some of the useful java 8 functional interfaces are Consumer, Supplier, Function and Predicate.
I hope you enjoyed by reading this article if any suggestion please write down below in comment section.


0 comments:

Post a Comment

 
Copyright © 2019 techfloaters • All Rights Reserved.
Template Design by Ashok Veer ( veersoft solution)