What is Functional Interface?
The functional interface is the only base for Lambda expression that is the lambda expression provides the implementation for the functional interface. The functional interface is a special type of interface that only contains a single abstract method and it is also known as SAM interface. In addition to a single abstract method, you can define any number of default and static methods within the functional interface.
Examples of a functional interface are all the interfaces present inside the package java.util.function and all those interfaces which are already present before Java 8 with a single abstract method like Runnable, Callable, Comparable, Comparator, etc.
What does @FunctionalInterface annotation?
The @FunctionalInterface annotation can guarantee that the interface has the only single abstract method and additionally it gives you an indication that this interface is a functional interface. It is like @Override annotation, which indicates that actually, you are overriding a method or not. It is not mandatory to apply the @FunctionlInterface annotation on the interface in Java 8 but it helps the compiler to identify that it is a functional interface. Here is a typical functional interface example.
Functional Interface with respect to Inheritance
As we know an interface can extend one or more than one interface at a time. Let suppose parent interface is a functional interface and child interface extending it and does not contain any abstract method. It means a child interface is also a functional interface as it inherits the parent abstract method. One more thing is that you can exactly define the same parent abstract method inside the child interface but if you change the signature of the method or define a new method inside the child interface then it will no longer a functional interface. Here is an example
Parent functional interface: Country.java
Child functional interface: InfectedCountry.java
If you noticed, inside the child interface InfectedCountry line number 17 is commented because if you uncomment that line then the java compiler will pop up an error message saying "Invalid '@FunctionalInterface' annotation; InfectedCountry is not a functional interface". That is why you have to apply @FunctionalInterface annotation on the functional interface so that the java compiler can check that there should not be more than a single abstract method.
Now if a functional interface extends more than one functional interface then there are two cases that come up. The first one is if both parent functional interface having an exact same abstract method and the second one is if both functional interfaces having different abstract method. Now in the first case, java 8 compiler would not complain any error but in the second case java 8 compiler shows you an error "Invalid '@FunctionalInterface' annotation; Covid19 is not a functional interface" in the child interface. Here is an example.
Parent functional interfaces: Sars.java and Mers.java
Child functional interface: Covid19.java
This is all about the functional interface. Thank you so much, guys. Please share this post with your friends and colleagues also. In the future, if I will find something new about functional interfaces, I will update this post. Please do comment if you have any queries or suggestions. I will respond to you back.
Comments
Post a Comment