Java 8 – Functional Interfaces

Learn about Java 8 functional interfaces and the rules around one abstract method permitted in one interface. Learn to add more methods via default methods in functional interfaces.

Table of Contents 

1. What is functional interface
2. Do's and Don't's in functional interfaces

1. What is functional interface

Functional interfaces are new additions in java 8 which permit exactly one abstract method inside them. These interfaces are also called Single Abstract Method interfaces (SAM Interfaces).

In Java 8, functional interfaces can be represented using lambda expressions, method reference and constructor references as well.

Java 8 introduces an annotation i.e. @FunctionalInterface too, which can be used for compiler level errors when the interface you have annotated violates the contracts of exactly one abstract method.

Let’s build our first functional interface:

@FunctionalInterface
public interface MyFirstFunctionalInterface 
{
	public void firstWork();
}

Let’s try to add another abstract method:

@FunctionalInterface
public interface MyFirstFunctionalInterface 
{
	public void firstWork();
	public void doSomeMoreWork();	//error
}

Above will result into compiler error as given below:

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

Functional-Interface-Error

Read More : Generic functional interfaces

2. Do’s and Don’t’s in functional interfaces

Below is list of things which are allowed and which are not in a functional interface.

  • As discussed above, only one abstract method is allowed in any functional interface. Second abstract method is not not permitted in a functional interface. If we remove @FunctionInterface annotation then we are allowed to add another abstract method, but it will make the interface non-functional interface.
  • A functional interface is valid even if the @FunctionalInterface annotation would be omitted. It is only for informing the compiler to enforce single abstract method inside interface.
  • Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. Since default methods are not abstract you’re free to add default methods to your functional interface as many as you like.

    Below is valid functional interface:

    @FunctionalInterface
    public interface MyFirstFunctionalInterface 
    {
        public void firstWork();
    
        default void doSomeMoreWork1(){
        //Method body
        }
    
        default void doSomeMoreWork2(){
        //Method body
        }
    }
    
  • If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere. e.g. Comparator is a functional interface even though it declared two abstract methods. Why? Because one of these abstract methods “equals()” which has signature equal to public method in Object class.

    e.g. Below interface is a valid functional interface.

    @FunctionalInterface
    public interface MyFirstFunctionalInterface 
    {
    	public void firstWork();
    
    	@Override
    	public String toString();                //Overridden from Object class
    
    	@Override
    	public boolean equals(Object obj);        //Overridden from Object class
    }
    

That’s all for this time regarding functional interfaces in java 8.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

15 thoughts on “Java 8 – Functional Interfaces”

  1. Thanks for the details
    # It will really help readers if we can add more scenarios when this particular feature can/should be used
    # Which current features it can replace or act as an alternative to
    # What would be the BIG-O or any performance metric we can back up with

    Reply
    • No A Functional Interface can not extends another Funtional Interface as the child funtional interface will be having two abstract method which will give exception.

      Reply
  2. I have doubt . What is the main purpose of implementing functional interfaces in java8 ?

    Thanks to you lokesh, now I know about functional interfaces but dont know when to use it ?

    Reply
  3. Nice one.This is absolutely clear to understand.
    But only one doubt in which scenario we will use Functional Interface/SAM Interface ?

    Thanks & Regards
    Tony

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.