Recent Posts

Saturday, 6 October 2018

Functional Interfaces Tutorial

     In this tutorial we are going to learn about what is functional interface and @FunctionalInterface Annotation in Java. Along with this, we will cover Lambda expressions in Java with examples.

So, let’s start with Java Functional Interface.

What is Functional interface?
     If an interface contain only one abstract method, such type of interfaces are called functional interfaces and the method is called functional method or single abstract method (SAM). 
E.g
1) Runnable : It contains only run() method
2) Comparable : It contains only compareTo() method
3) ActionListener : It contains only actionPerformed()
4) Callable : It contains only call() method
     Inside functional interface in addition to single Abstract method (SAM) we write any number of default and static methods.
E.g
interface Interf { 
   public abstract void m1(); 
   default void m2() { 
      System.out.println("Hi this is Ashok..!!"); 
   } 
}
     In Java 8, Sun Micro System introduced @FunctionalInterface annotation to specify that the interface is Functional Interface.
@FunctionalInterface
Interface Interf { 
   public void m1();
}
     Inside Functional Interface we can take only one abstract method, if we take more than one abstract method then compiler raise an error message that is called we will get compilation error.
@FunctionalInterface
interface Interf { 
   public void m1(); 
   public void m2();
}
This code gives compilation error.
     Inside Functional Interface we have to take exactly only one abstract method.If we are not declaring that abstract method then compiler gives an error message.
@FunctionalInterface 
interface Interf { 

}
This code gives compilation error.
Functional Interface with respect to Inheritance
     If an interface extends Functional Interface and child interface doesn’t contain any abstract method then child interface is also Functional Interface
@FunctionaInterface
interface A {
   public void m1();
}

@FunctionalInterface
interface B extends A { 
}
In the child interface we can define exactly same parent interface abstract method.
@FunctionaInterface
interface A {
   public void m1();
}

@FunctionalInterface
interface B extends A { 
   public void m1();
}
     In the child interface we can’t define any new abstract methods otherwise child interface won’t be Functional Interface and if we are trying to use @Functional Interface annotation then compiler gives an error message.
@FunctionaInterface
interface A {
   public void m1();
}

@FunctionalInterface
interface B extends A { 
   public void m2();
}
This code gives compilation error.
@FunctionaInterface
interface A {
   public void m1();
}

interface B extends A { 
   public void m2();
}
This’s Normal interface so that code compiles without error.
     In the above example in both parent & child interface we can write any number of default methods and there are no restrictions. Restrictions are applicable only for abstract methods.

Functional Interface Vs Lambda Expressions 
     Once we write Lambda expressions to invoke it’s functionality, then Functional Interface is required. We can use Functional Interface reference to refer Lambda Expression. Where ever Functional Interface concept is applicable there we can use Lambda Expressions.
Without Lambda Expression
interface Interf {
   public void m1();
}

public class Demo implements Interface {
   public void m1() {
      System.out.println(“method m1 execution”); 
   }
}

public class Test {
   public static void main(String[] args) { 
      Interf i = new Demo(); 
      i.m1(); 
   }
}
Above code With Lambda expression
interface Interf { 
   public void m1();
}   
class Test { 
   public static void main(String[] args) { 
      Interf i = () -> System.out.println(“Method m1 Execution”); 
      i.m1();
   }
}
Example 2:
Without Lambda Expression
interface Interf { 
   public void sum(int a,int b); 
}
class Demo implements Interf {
   public void sum(int a,int b) { 
      System.out.println(“The sum:”+(a+b)); 
   } 
}
public class Test {
   public static void main(String[] args) { 
      Interf i = new Demo(); 
      i.sum(20,5); 
   }
}
With Lambda Expression 
interface Interf { 
   public void sum(int a,int b);
}   
class Test { 
   public static void main(String[] args) { 
      Interf i = (a, b) -> System.out.println(“The sum:”+(a+b)); 
      i.sum(10,20);
   }
}
Anonymous inner classes vs Lambda Expressions
    Wherever we are using anonymous inner classes there may be a chance of using Lambda expression to reduce length of the code and to resolve complexity.
Example
With anonymous inner class
class Test { 
   public static void main(String[] args) { 
      Thread t = new Thread(new Runnable() { 
         public void run() { 
           for(int i=0; i<10; i++) { 
              System.out.println("Child Thread"); 
           } 
         }
      });
      t.start();
      for(int i=0; i<10; i++)
         System.out.println("Main thread"); 
   }
}
With Lambda expression
class Test { 
   public static void main(String[] args) { 
      Thread t = new Thread(() -> { 
         for(int i=0; i<10; i++) { 
            System.out.println("Child Thread"); 
         } 
      });
      t.start();
      for(int i=0; i<10; i++)
         System.out.println("Main thread"); 
   }
}
What are the advantages of Lambda expression?
1. We can reduce length of the code so that readability of the code will be improved.
2. We can resolve complexity of anonymous inner classes.
3. We can provide Lambda expression in the place of object.
4. We can pass lambda expression as argument to methods.
Note
1. Anonymous inner class can extend concrete class, can extend abstract class, can implement interface with any number of methods but
2. Lambda expression can implement an interface with only single abstract method (Functional Interface).
3. Hence if anonymous inner class implements Functional Interface in that particular case only we can replace with lambda expressions. Hence wherever anonymous inner class concept is there, it may not possible to replace with Lambda expressions.
4. Anonymous inner class! = Lambda Expression
5. Inside anonymous inner class we can declare instance variables.
6. Inside anonymous inner class “this” always refers current inner class object(anonymous inner class) but not related outer class object

Example
1. Inside lambda expression we can’t declare instance variables.
2. Whatever the variables declare inside lambda expression are simply acts as local variables
3. Within lambda expression ‘this” keyword represents current outer class object reference (that is current enclosing class reference in which we declare lambda expression).
interface Interf { 
   public void m1(); 
}
class Test {
   int x = 777;
   public void m2() { 
      Interf i = () -> { 
         int x = 888;
         System.out.println(x); 888 
         System.out.println(this.x); 777 
      }; 
      i.m1(); 
   } 
   public static void main(String[] args) { 
      Test t = new Test(); 
      t.m2();
   }
}
☀ From lambda expression we can access enclosing class variables and enclosing method variables directly.
☀ The local variables referenced from lambda expression are implicitly final and hence we can’t perform re-assignment for those local variables otherwise we get compile time error
interface Interf { 
   public void m1(); 
}
class Test {
   int x = 50;
   public void m2() { 
      Interf i = () -> { 
         int y = 100;
         System.out.println(x); 50
         System.out.println(y); 100
         x = 200;
         y = 300; // C.E
      }; 
      i.m1(); 
   } 
   public static void main(String[] args) { 
      Test t = new Test(); 
      t.m2();
   }
}
Differences between anonymous inner classes and Lambda expression

Next Tutorial : Default Methods in Java 8

Previous Tutorial : Lambda Expressions Tutorial
 

1 comment: