What are Functional Interfaces and Functional Descriptor?

There is no Java developer who is not familiar with these Interfaces which contain only one method. If you are not familiar, no worries I will in the course of this article throw some light on such interfaces.

also read:

Those who have created GUI applications using Swing/AWT would be familiar with ActionListener interface, those working on mutli threaded applications or concurrent applications would be familiar with Runnable, Callable, Executor and other interfaces. Those who worked on sorting of objects would be familiar Comparator interface. What is it that is common between them? Yes, you got it right, they are interfaces with one method (not considering the methods it inherits from the Object class). These were called Single Abstract Method classes, its were because with Java 8 these are called as Functional Interfaces.

Runnable:

public interface Runnable {
    public abstract void run();
}

Callable:

public interface Callable<V> {
    V call() throws Exception;
}

Executor:

public interface Executor {
    void execute(Runnable command);
}

Functional Interfaces can also have abstract methods which are inherited from Super interfaces and those should be override equivalent (read more about override equivalent methods here). For example:

interface I1{
  void method1(List<Number> n);
}
interface I2{
  void method1(List n);
}
interface I3 extends I1, I2{} // Valid Functional Interface.
interface I4{
  int method1(List n);
}
interface I5 extends I2,I4{} //Not a functional interface

The bottom line is that functional interfaces are those that have single abstract methods (apart from the inherited public methods from Object class). The single method might not be the only method, but can be inherited from multiple interfaces but they signatures are equivalent to each other (See Override equivalent) such that those multiple methods actually represent/act as a single method.

Along with the functional interfaces, there’s a concept called functional descriptors associated. Functional descriptor of an Interface is the method type of the single abstract method of the interface. Here the method type of a method includes: argument types, return type and the throws clause.
In our above examples the functional descriptors are

//For Runnable
() -> void
//For Executor
(Runnable) -> void
//For Interface I3
(List<String>) -> void

These concepts are used while constructing lambda expressions and using them. Just to give a brief: all these functional interfaces can be represented by using Lambda expressions, the compiler in turn infers the context in which the lambda expression is used and uses the corresponding Functional Interface.

Comments

comments

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Trackbacks

  1. [...] proceeding further its good to read about Functional Interfaces and Defender Methods to some [...]

  2. JavaPins says:

    What are Functional Interfaces and Functional Descriptor?…

    Thank you for submitting this cool story – Trackback from JavaPins…

Speak Your Mind

*