A functional interface is any interface with only one method in it.
Remember!
Java API classes already uses functional interfaces, examples includes :
1.
2.
1.
java.lang.Runnable
with only a run() method2.
java.util.Comparator
with only a compare() methodNote that the interface can include methods from object class like
equals(), hashCode()
etc which are not considered as methods of the interface. In order for an interface to be called a functional interface, it must have defined methods of its own.Java 8 has introduced a new annotation called
@FunctionalInterface
. This annotation is used to check for compile time type safety on functional interfaces. See the below example :loading...
Output:
Hit the pig using Red Angry Bird...
Hit the pig using Red Angry Bird...
In this example, there is a functional interface named AngryBird. This has only one method
hitThePig()
. AndRedAngryBird
is a class that implements this interface and provides the implementation.In the
main()
method, we tried calling the functional interface method using outer class instance. Dont get confused by the syntax, we are just calling the inner class(RegAngryBird
) using the Outer class instance, functionalInterfaceExample1.Now, where did we use the functional interface annotation. Well, the functional interface is already did its task, compile time type safety. It already checked to see if there is only one method in the interface or not. Since we had only one method all went well.
Let us try adding one more method to this interface and see what happens... Try adding the below method to the AngryBird interface.
public void hitThePigAgain();
You will get the below compiler error:
Output:
Invalid '@FunctionalInterface' annotation; FunctionalInterfaceExample1.AngryBird is not a functional interface
Invalid '@FunctionalInterface' annotation; FunctionalInterfaceExample1.AngryBird is not a functional interface
Remember!
A functional interface is an interface which can have one and only one method inside it.
Ok, now what if we have an interface which has more than one method in it but still lambda expressions could be used. Since we do not know what lambda expressions are yet, consider that we want to use only one method from a functional interface which has multiple methods. How to acheive this ?
The same challenge was faced by java api creators while extending collections api to support lambda expressions. To facilitate this, Java 8 has introduced default methods/defender methods/virtual extension methods. Consider the below example,
loading...
Output:
Hit the pig using Red Angry Bird...
Hit the pig using default Angry Bird...
Hit the pig using Red Angry Bird...
Hit the pig using default Angry Bird...
From the above example, we can see that there is another method added to AngryBird called
hitThePigAgain()
. Observe the default keyword before the method declaration. Remember!
Dont get confused between default access modifier and default keyword. Remember that default access modifier does not have any keyword. For example, the below method's access modifier is default and it is also a default/defender method.
loading...
We will see a realtime usage of default method in Collections API while using Stream.
For now, just remember that default method allows us to use the interface as a functional interface by providing default implementation.
Note that, by using default methods, we are actually adding implemenation to interface which are supposed to contain only abstract methods. Yes, from Java 8 onwards, interfaces can contain implementation via default methods.
Thats all folks !! Happy coding. If you feel this helped you, keep supporting us by or or below or on the articles on social media.