Friday, 30 September 2016

Java 8 | Functional Interface

What is the functional interface?
Functional interfaces are new features in Java 8 which permit exactly one abstract method inside them. These interfaces are also called Single Abstract Method(SAM) interfaces. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A functional interface can have any number of default methods. Runnable, ActionListener, Comparable are some of the examples of functional interfaces.

Example of Functional Interface In Java 8?
We are creating the thread using the Runnable (a functional Interface) in Java 8.

package com.java8.lambda;
/**
 * @author rajesh.dixit
 * @since Aug 21, 2018 10:26:08 AM
 */
public class MyThread {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Runnable runnable = () -> {
            System.out.println("Thread has been started!");
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }
}

@FunctionalInterface Annotation
Java 8 introduces an annotation i.e. @FunctionalInterface, which can be used for compiler level errors when the interface we have annotated violates the contracts of Functional Interface.

@FunctionalInterface
interface MyFunctionalInterface {
     public void onlyMethod();
    
}

It will throw error while defining another method in the functional interface.

Unexpected @FunctionalInterface annotation
@FunctionalInterface ^ MyFunctionalInterface is not a functional interface
multiple non-overriding abstract methods found in interface MyFunctionalInterface


java.util.function package @FunctionalInterface
The java.util.function package in Java 8 contains many functional interfaces:

Predicate: The Predicate interface has an abstract method test which gives a Boolean value as a result of the specified argument.

public Predicate {
    public boolean test(T t);
}

BinaryOperator: The BinaryOperator interface has an abstract method apply which takes two arguments and returns a result of the same type.

public interface BinaryOperator {
     public T apply(T x, T y);
}

Function: The Function interface has an abstract method apply which takes an argument of type T and returns a result of type R.

public interface Function {
   public R apply(T t);
}

The important points about functional interfaces
1) Conceptually, a functional interface has exactly one abstract method. The second abstract method is not permitted in a functional interface.

A functional interface is valid even if the @FunctionalInterface annotation would be omitted. It is only for informing the compiler to enforce single abstract method inside interface.  If we remove @FunctionalInterface annotation then we are allowed to add another abstract method, but it will make the interface non-functional interface.

@FunctionalInterface
interface MyFunctionalInterface {
     public void onlyMethod();

     default void defaultMethod() {
           System.out.println("Default method ");
     }
}

Since default methods have an implementation, they are not abstract. Since default methods are not abstract you’re free to add default methods to your functional interface as many as you like.

2) If an interface declares an abstract method overriding one of the public methods of java.lang.Object that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.


Comparator is a functional interface even though it declared two abstract methods. Why?
If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implemmation from java.lang.Object or elsewhere.
Since equals is a public method of Object, this statement applies; thus, for Comparator only the compare method contributes to the abstract method count.




No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...