Functional Interface

Definition : Any interface which has one and only one Abstract Method is called Functional Interface.

Built in functional Interfaces are

1)Comparator Interface which has only one abstract method i.e. compare(T arg1,T arg2)
Where T is the generic datatype.
2)Runnable Interface which has single abstract method i.e. run()
Like this there are many others.
Note : functional interface can have an optional annotation @FunctionalInterface on top of interface name. This helps compiler to detect error if interface does not satisfy Functional interface definition.

Now we will create our own functional interface.

1
2
3
4
5
6
Ex1
package com.kb.Lambda;
 
public interface FunctionalInterfaceEx1 {
void show();
}
Ex1
package com.kb.Lambda;

public interface FunctionalInterfaceEx1 {
void show();
}

Ex2

1
2
3
4
5
6
package com.kb.Lambda;
 
@FunctionalInterface 
public interface FunctionalInterfaceEx1 {
void show();
}
package com.kb.Lambda;

@FunctionalInterface 
public interface FunctionalInterfaceEx1 {
void show();
}

Invalid Functional interface

1
2
3
4
5
@FunctionalInterface 
public interface FunctionalInterfaceEx1 {
void show();
void print();
}
@FunctionalInterface 
public interface FunctionalInterfaceEx1 {
void show();
void print();
}

It is invalid because it has 2 abstract methods in it.
And since we have @FunctionalInterface annotation on top of interface, it will throw compile time error.

1
2
3
4
public interface FunctionalInterfaceEx1 {
void show();
void print();
}
public interface FunctionalInterfaceEx1 {
void show();
void print();
}

It is not functional interface but compiler treat this as a normal interface as we don’t have any annotation on top of interface so no error. But it is not a valid functional interface.

Now come back to valid functional interface

1
2
3
4
5
package com.kb.Lambda;
 
public interface FunctionalInterfaceEx1 {
void show();
}
package com.kb.Lambda;

public interface FunctionalInterfaceEx1 {
void show();
}

Now we can write as below using existing old way of Java feature

1
2
3
4
5
6
7
8
FunctionalInterfaceEx1 ex1 = new FunctionalInterfaceEx1() {
    
    public void show() {
        System.out.println("Hai welcome");      
    }
    };
    
    ex1.show();
FunctionalInterfaceEx1 ex1 = new FunctionalInterfaceEx1() {
	
	public void show() {
		System.out.println("Hai welcome");		
	}
	};
	
	ex1.show();

O/p is

Now observe carefully how we can write the above code in a much simpler way

1
2
3
FunctionalInterfaceEx1 ex2 = () -> System.out.println("Hai welcome in new way");
        
    ex2.show();
FunctionalInterfaceEx1 ex2 = () -> System.out.println("Hai welcome in new way");
		
	ex2.show();

O/p is

It should be understood as below
InterfaceName referenceVariable = signature of a method inside the interface -> {body of the method}

This kind of implementing anonymous class can be achieved in a very simple way from Java 8 onwards i.e. by using Lambda Expression.

Yes the above syntax is a new feature of Java 8 -> Lambda expression.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on