JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

What are Functional Interfaces and Functional Descriptor?

May 20, 2012 by Mohamed Sanaulla Leave a Comment

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:

  • Java 8.0 Tutorials
  • Java 7.0 Tutorials
  • New Features in Java 7.0
  • G1 Garbage Collector in Java 7.0

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:

[code lang=”java”]
public interface Runnable {
public abstract void run();
}
[/code]

Callable:

[code lang=”java”]
public interface Callable<V> {
V call() throws Exception;
}
[/code]

Executor:

[code lang=”java”]
public interface Executor {
void execute(Runnable command);
}
[/code]

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:

[code lang=”java”]
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
[/code]

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

[code lang=”java”]
//For Runnable
() -> void
//For Executor
(Runnable) -> void
//For Interface I3
(List<String>) -> void
[/code]

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.

Filed Under: Java Tagged With: Java 8, Project Lambda

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.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved