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

Java 8 – Functional Interfaces

November 12, 2014 by Krishna Srinivasan Leave a Comment

What are Functional Interfaces?

Functional interfaces are those interfaces that has single abstract method. According to Java 8 Specification, interface can be known as functional interface if there is a single method that is compliant with the below rules:

  • If an interface has single abstract method, then it is known as the functional interface.
  • A functional interface can have any number of default methods with single abstract method. Default methods are method declaration inside interfaces with the keyword “default”. This is one of the new feature from Java 8.
  • If an interface extends another functional interface and not declaring any other methods in it, then it is considered as functional interface since it inherits single abstract method from the super interface.
  • @FunctionalInterface  annotation used by the compiler to detect if an interface is valid functional interface. It is not compulsory to use @FunctionalInterface annotation to indicate a functional interface.
  • With addition to the single abstract method, it can declare any of the methods in Object class as abstract and still it is a valid functional interface.
  • These interfaces are previously known as the Single Abstract Method Interfaces (SAMI).
  • Few examples for the functional interfaces are, java.lang.Runnable, java.awt.event.ActionListener, java.util.Comparator, java.util.concurrent.Callable

Examples for Functional Interfaces

  • What are Functional Interfaces and Functional Descriptor?
  • Lambda Expressions in Java 8

Lets look at few examples that is part of Java API.

Runnable and Comparator interfaces is an example for the functional interface.

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

Comparator.java

[code lang=”java”]
interface Comparator<T> {
boolean equals(Object obj);
int compare(T o1, T o2);
}
[/code]

Lets look at the below user defined interfaces with the functional interface semantics.

Deployable.java

[code lang=”java”]
package net.javabeat;
public interface Deployable {
boolean someWork(Iterable<String> arg);
}
[/code]

Integrateable.java

[code lang=”java”]
package net.javabeat;

public interface Integrateable {
boolean someWork(Iterable<String> arg);
}
[/code]

Useable.java

[code lang=”java”]
package net.javabeat;
@FunctionalInterface
public interface Useable extends Deployable, Integrateable{
}
[/code]

  • Useable is a functional interface because while it inherits two methods from Deployable and Integrateable. If you look at the both the interfaces, the methods have the same signature..So the inherited methods logically represent a single method.
  • @FunctionalInterface annotation can be used at the top of the interface declaration.

Deployable.java

[code lang=”java”]
package net.javabeat;

public interface Deployable {
Iterable someWork(Iterable<String> arg);
}
[/code]

Instance Creation for Functional Interface

To create instances from your functional interfaces you have three ways:

  1. Create a class that implements your functional interface and just make an instance of that defined class. (Used Prior to Java 8).
  2. Create a method reference expression (Anonymous Instance, Used Prior to Java 8).
  3. Using Lambda expressions (New for Java 8).

Look at the below examples that would create instance of a given functional interface by using first and second options. Third option will be explained in the Lambda Expression section.

SomeInterface.java

[code lang=”java”]
package net.javabeat;
public interface Deployable {
Integer someWork();
}
[/code]

UIComponent.java

[code lang=”java”]
package net.javabeat;
public class UIComponent {
public static void main(String [] args){
Deployable deployable = new Deployable() {
@Override
public Integer someWork() {
return 1;
}
};
System.out.println("Invoke SomeWork Method Against SomeInterface :: "+deployable.someWork());
}
}
[/code]

The output will be:

[code lang=”xml”]
Invoke SomeWork Method Against SomeInterface :: 1
[/code]

Filed Under: Java Tagged With: Java 8

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

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