Skip to main content

Functional Interface

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

Popular posts from this blog

How to add an existing project to a GitHub Repository

  Add Existing project to a GitHub Repository Step 1: Login into your GitHub account and create a new repository. Step 2: Go to your project folder on your machine and open git bash. After that runs the command “ git init ”. This command initialized the empty git repository on your local machine. Step 3: Create a file “. gitignore ” using the command “ echo > .gitignore ”. Add the file extension or file name inside this file, those you don’t want to commit to your repository. Git will ignore all those files that will be present inside this file. Step 4: Add all the files to the staging area using the command “git add .” This command adds all the files to the staging area and all these files are now ready for the commit. And with the help of the command “git status” , you can check that all the files are inside the staging area. Also, you can see the file extension or the file name mentioned inside the “.gitignore”  are ignored by git. Step 5: Commit the files that are pre...

Lambda Expression

What is Lambda Expression? In java 8, a powerful concept is added named Lambda Expression.  Lambda expression is an anonymous function (a nameless function that does not have a modifier, return type, and even the type of its arguments).  Lambda expression is that you can pass to a method whose argument type is functional interface i.e. an interface that is either annotated by @FunctionalInterface  annotation e.g. interfaces from java.util.function package, or an interface with a single abstract method e.g. Comparable, Comparator, Runnable, Callable, ActionListener, etc.  Lambda expression allows passing customized code to the java method that was not possible before java 8. Although before Java 8 the only option was to wrap your code as an object and pass it using anonymous inner class to the method. Lambda expression defines parameters and code that executed using those parameters. Let's take the simplest example of the lambda expression (String input) -> ...

How to install IntelliJ IDEA on Windows?

System Requirements and steps to install IntelliJ IDEA on Windows System Requirements:  Step 1: Download IntelliJ IDEA. Click here. Step 2: Run the downloaded exe file and click on next. Step 3: Check the checkboxes mentioned in the below image and click on next and then hit the install button. Step 4: Final step is to reboot your system before start using IntelliJ IDEA. Thank you, guys. Please do comment below if there is any query or feedback.