Java Functional Interface has become available for us since the introduction of new features in Java 8. Needless to say, how important functional Interfaces are in Java. If we start learning the most popular feature of Java 8 : ‘The Lambda Expression’, we should at least know the basics of Functional Interfaces. However, you will get more than basics from this article.
Moreover, they also promote functional programming in Java. Without functional Interfaces, you can’t imagine to write a code of Lambda Expression and Method References. In fact, introduction of default & static methods inside an interface in Java 8 itself has completely changed the definition of Interfaces in Java. Let’s discuss our topic ‘Java Functional interface’ and other related concepts.
How many types of Interfaces are there in Java ?
Typically we have three types of Interfaces till now.
1) Normal Interface
2) Marker Interface
3) Functional Interface
Normal Interface is an interface which has either one or multiple number of abstract methods.
However, Marker Interface is an interface with no abstract method. (Because till JDK8 an interface can have only abstract methods, no normal methods). We use it to provide a type to its sub-classes for executing some logic from other classes.
Additionally, Functional Interface is an interface which has only one abstract method. Further, it can have any number of static, default methods and even public methods of java.lang.Object class. In fact, we use it to define Lambda Expressions & Method References by directly providing logic to an argument of a method.
If you want to learn interfaces in java in detail, kindly visit our separate article on ‘Java Interface‘, which includes new changes in interfaces after the introduction of Java 8 & Java 9 features.
What is a Java Functional Interface?
If an interface contains only one abstract method, we call it a functional interface. The contained method is called functional method or single abstract method (SAM). Consequently, they provide target types for lambda expressions and method references. Moreover, Java 8 contains some built-in Functional Interfaces designed for commonly occurring use cases. They are available in a separate package java.util.Function. Hence, we don’t have to create our own functional interfaces every time for a small use case. Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.
Note : In addition to single abstract method, we can also have any number of default & static methods inside a Functional Interface. Moreover, we don’t have any restrictions on including ‘Object’ class’ public methods inside Functional interfaces. Needless to say, they are equals(), hashCode(), notify(), notifyAll(), toString(), wait(), getClass() .
Is it not mandatory to add abstract keyword during method declaration in functional interface as we generally do in interfaces before JDK 1.8?
Obviously, It is not mandatory to add abstract keyword during the method declaration either in a java functional interface or in a normal java interface. Even, before JDK 1.8 also, it was optional. When we apply interface keyword before the name of the Interface, all methods declared in this interface become public abstract by default. Therefore, it’s up to the developer whether he/she adds it or not.
Why Java Functional Interface? Advantages of Java Functional Interface?
We provide implementation of Functional Interfaces using Lambda expressions & Lambda expressions can only be used in context of Functional interfaces. You can assign a lambda expression to a single abstract method (i.e. To a Functional interface). If you could assign a lambda expression to an interface containing more than one abstract method (i.e. a non functional interface), the lambda expression could only have implemented one of its methods, leaving the other methods unimplemented. That’s why Lambda expressions don’t have method name, return types etc. as it talks about a single abstract method.
Therefore, there is no point of having more than one abstract method in a Java Functional Interface as they are meant for Lambda expressions & Lambda expression could be assigned to a single abstract method.
Examples of Java Functional Interface:
Runnable : contains only run() method
Comparable : contains only compareTo() method
ActionListener : contains only actionPerformed()
Callable : contains only call() method
ItemListener : contains only itemStateChanged() method
How to write a Java Functional Interface?
In addition to having only one abstract method, we should write @FunctionalInterface annotation in order to let the compiler know that the interface is a Functional. In fact, if you add annotation @FunctionalInterface, the compiler will not let you add any other abstract method inside it. For example, observe the below code:
Behavior Of Functional Interface in the context of Inheritance
If an interface extends Functional Interface and child interface doesn’t contain any abstract method, then again child interface is also a Functional Interface.
In the child interface we are allowed to define exactly same single abstract method as it is in the parent interface as below.
In other words, the child interface will not have any new abstract methods otherwise child interface won’t remain a Functional Interface and if we try to use @FunctionalInterface annotation in child interface, compiler will show an error message. If we don’t use @FunctionalInterface in child interface & declare new abstract method, the compiler will consider it as a normal interface & will not show any error as below.
Functional Interfaces in context of Lambda Expression (Functional Interfaces with Lambda Expression)
We can assign Lambda expressions to Functional Interfaces. In brief, if we have an interface with a single abstract method or a functional interface, then we can use the concept of lambda. This is a prerequisite to apply the concept of Lambdas. Now let’s look at the code below:
We can write equivalent code to the above code using lambda expression as below:
Example to convert traditional code to the code using Lambda Expression
Let’s take another example to convert code using lambda expressions. To illustrate, we will develop a functionality to calculate sum of two numbers to understand use of Lambda expressions.
Without Lambda Expression:
With Lambda Expression:
Is it not mandatory to add abstract keyword during method declaration in functional interface as we generally do in interfaces before JDK 1.8?
FAQ
Can a functional interface have multiple default or static methods?
Yes, a functional interface can have multiple default or static methods. These methods do not have any impact on its status as a functional interface.
What is the significance of the @FunctionalInterface annotation?
The @FunctionalInterface annotation is optional but recommended. It acts as a documentation and helps the compiler to identify a functional interface. It also prevents accidental addition of more abstract methods.
Can we use functional interfaces with method references as well?
Yes, functional interfaces are intended to be used with lambda expressions and method references. They allow us to write more concise and expressive code.
For further details on functional interfaces, kindly visit Oracle Documentation.
What DO you mean by this statement? why to mention ‘even methods of java.lang.Object class’ what does this mean?
Functional Interface is an interface which has only one abstract method. Further it can have any number of static, default methods and even methods of java.lang.Object class
It simply means ‘Apart from single abstract method you can also have methods of java.lang.Object class in a Functional Interface’.
amazing content on java 8. Much appreciated 🙂