Java 8 – Functional Interface

November 14, 2018 Off By ravi24kant

A functional interface is an interface with only 1 abstract method. this suggests that the interface implementation can only represent one behaviour. samples of a functional interface in Java are:

java.util.Comparator

java.lang.Runnable

java.util.concurrent.Callable

java.io.FileFilter

An important purpose to remember is that the functional interface can have a variety of default methods however only 1 abstract method.

Java 8 introduces AN annotation i.e. @FunctionalInterface too, which may be used for compiler level errors once the interface you’ve got annotated violates the contracts of functional Interface.

Functional Interface Example:

package functionalInterfaceForMe; 
@FunctionalInterface 
public interface MyFunctionalInterface { 
public void goForWork(); 
}

Let’s insert another abstract interface in it:

package functionalInterfaceForMe;
@FunctionalInterface
public interface MyFunctionalInterface {
public void goForWork();
public void doYourWork();
}

This will result in compile time error as stated below:

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

List of things that are allowed and that are not allowed in a functional interface.

  •  As mentioned above, just one abstract method is allowed in any functional interface. The second abstract method isn’t allowable in a functional interface. If we remove @FunctionInterface annotation then we are allowed to add another abstract method, however, it’ll build the interface non-functional interface.
  • A functional interface is valid though the @FunctionalInterface annotation would be omitted. it’s just for informing the compiler to enforce a single abstract method within an interface.
  • Conceptually, a functional interface has precisely one abstract method. Since default methods have AN implementation, they’re not abstract. Since default methods are not abstract you’re liberal to add default methods to your functional interface as several as you wish.
  • Below is valid functional interface:
package functionalInterfaceExample;

@FunctionalInterface
public interface MyFunctionalInterface
default void doSomeMoreWork()
}
  • If AN interface declares an abstract method overriding one among the general public methods of java.lang.Object, that also doesn’t count toward the interface’s abstract method count since any implementation of the interface can have an implementation from java.lang.Object or elsewhere. e.g. Comparator may be a functional interface although it declared 2 abstract methods. Why? because one among these abstract methods “equals()” that has signature up to public method in Object class.
  • e.g. Below interface may be a valid functional interface.
package functionalInterfaceExample;

@FunctionalInterface
public interface MyFunctionalInterface {
public void doWork();
@Override
public String toString(); //Overridden from Object class
@Override
public Boolean equals(Object obj); //Overridden from Object class
}