• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • 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)
  • 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)

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.

interface Runnable {
        void run();
}

Comparator.java

interface Comparator<T> {
        boolean equals(Object obj);
        int compare(T o1, T o2);
}

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

Deployable.java

package net.javabeat;
public interface Deployable {
	boolean someWork(Iterable<String> arg);
}

Integrateable.java

package net.javabeat;

public interface Integrateable {
	boolean someWork(Iterable<String> arg);
}

Useable.java

package net.javabeat;
@FunctionalInterface
public interface Useable extends Deployable, Integrateable{
}
  • 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

package net.javabeat;

public interface Deployable {
	Iterable someWork(Iterable<String> arg);
}

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

package net.javabeat;
public interface Deployable {
	Integer someWork();
}

UIComponent.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());
	}
}

The output will be:

Invoke SomeWork Method Against SomeInterface :: 1

Category: JavaTag: 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.

Previous Post: «jquery JQuery Enabled Selector Example
Next Post: JQuery ajaxStart Example jquery»

Reader Interactions

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.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

EJB 3.0 Timer Services

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact