Functional Interface

If 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

Example : compare(T arg1,T arg2) , Where T is the generic datatype.

2) Runnable Interface which has single abstract method

Example : 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.


Example 1 :

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

public interface FunctionalInterfaceEx1 {
void show();
}


Example 2 :

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

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


Invalid Functional interface

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


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

  1. public interface FunctionalInterfaceEx1 {
  2. void show();
  3. void print();
  4. }
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. package com.kb.Lambda;
  2.  
  3. public interface FunctionalInterfaceEx1 {
  4. void show();
  5. }
package com.kb.Lambda;

public interface FunctionalInterfaceEx1 {
void show();
}


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

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


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

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



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 by using
Lambda Expression

.

About the Author

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