Showing posts with label Java 8. Show all posts
Showing posts with label Java 8. Show all posts

Tuesday, May 24, 2016

Functional Interface in Java 8

An interface which includes an abstract method, default method , static method and can also override Object class methods.


public interface FunctionalInterface {

     // Only abstract method.
     void abc(Collection collection);

     // Functional interface can have more than one static or default methods.
     default void print() {
           System.out.println("This is default in Functional  Interface.");
     }
    
     //An interface can also have static helper methods from Java 8 onwards.
     static void abc() {
           System.out.println("Hey");
     }

     // And also can override methods of java.lang.Object
     @Override
     public String toString();

}

default() method in Java 8

Earlier interfaces have only methods without body/implementation. That means Classes implementing that interface must have implement that method.

From java8, with introduction of default keyword, one can give default implementation in interface itself which means if implementation is not there in concrete class, JVM will by default take default implementation.

public class DefaultMethod implements InterfaceA {

     public static void main(String[] args) {
           DefaultMethod df = new DefaultMethod();
           df.sayHi();
     }

     @Override
     public void saySomething() {
     }

}

interface InterfaceA {
     public void saySomething();

     /* Earlier we used to provide implementation in concrete class only.
      * By Adding the keyword default before the method’s access modifier, we do
      * not have to provide implementation for the method sayHi() in class
      * DefaultMethod.
      */

     default public void sayHi() {
           System.out.println("Hi frm InterfaceA. Not implemented in Class");
     }
}

Output:
Hi from InterfaceA. Not implemented in Class


What if there are two interfaces with same default method? How will concrete class call one interface default method?

public class DefaultMethod implements InterfaceA, InterfaceB {

     public static void main(String[] args) {
           DefaultMethod df = new DefaultMethod();
           df.sayHi();
     }

     @Override
     public void saySomething() {
     }
    
     /* Duplicate sayHi() in both Interface,
     To overcome from diamond problem,must override sayHi()*/
    
     @Override
     public void sayHi() {
           System.out.println("Implemented in main class DefaultMethod");
           InterfaceA.super.sayHi();
           InterfaceB.super.sayHi(); 
     }
}

interface InterfaceA {
     public void saySomething();

     /* Earlier we used to provide implementation in concrete class only.
      * By Adding the keyword default before the method’s access    modifier, we do not have to provide implementation for the method sayHi() in class DefaultMethod.
      */

     default public void sayHi() {
           System.out.println("Hi frm InterfaceA. Not implemented in Class");
     }
}

interface InterfaceB {
     default public void sayHi() {
           System.out.println("Hi frm InterfaceB. Not implemented in Class");
     }
}

Output:
Implemented in main class DefaultMethod
Hi frm InterfaceA. Not implemented in Class

Hi frm InterfaceB. Not implemented in Class