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 :
- package com.kb.Lambda;
- public interface FunctionalInterfaceEx1 {
- void show();
- }
package com.kb.Lambda; public interface FunctionalInterfaceEx1 { void show(); }
Example 2 :
- package com.kb.Lambda;
- @FunctionalInterface
- public interface FunctionalInterfaceEx1 {
- void show();
- }
package com.kb.Lambda; @FunctionalInterface public interface FunctionalInterfaceEx1 { void show(); }
Invalid Functional interface
- @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 , since we have @FunctionalInterface annotation on top of interface,
So it will throw compile time error.
- 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
- 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
- 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();
Now observe carefully how we can write the above code in a much simpler way
- FunctionalInterfaceEx1 ex2 = () -> System.out.println("Hai welcome, in new way");
- 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
.
its very simple and useful article.
Please add stream API also.
KB
Nice blog to read
– Chethu
Thank you Chethu 🙂
Really enjoyed the simple way articles
Thank you !!
One important difference between Lambda Expressions and Method reference is lambda expressions let us define anonymous methods which can be used as an instance of functional interfaces. Method references provide a way to refer existing methods.
Read more here – http://netjs.blogspot.com/2015/06/method-reference-in-java-8.html
Hi Infoj, Thanks !!! you are right, same thing is explained in the lambda expression posts in my blog.
ultimate