Hello readers, as you have visited here, you must be aware of Interfaces in Java. Before Java 8, all methods defined in an interface are public and abstract by default. After Java 8, there were certain enhancements in interfaces. Amongst all the enhancements in Java 8, one of them is “Functional Interface”.
What are Functional Interfaces?
In Java, the term functional interface refers to those interfaces which have exactly one abstract method. In addition to that abstract method, they can have any number of default and static methods in them. If you want to know about static and default methods, you can refer to JournalDev’s Article on static method and default method. The functional interfaces are also known as “Single Abstract Method (SAM) interfaces”.
Annotating these interfaces with
@FunctionalInterface helps IDE or compiler to interpret them at development-time, or by the framework at run-time.
Functional Interface with respect to Inheritance
If an interface with no abstract method extends a functional interface, then the extended interface will also be considered as functional interface as shown in the example below:
Again, if an interface with exactly one abstract method extends a functional interface with the same method signature, then the extended interface will also be considered as functional interface. Here’s an example below:
Predefined Functional Interfaces
The concept of functional interfaces was introduced in Java 8, but they were present before Java 8 also. Some of them are:
- Runnable –
- Callable<V> –
- Comparable –
int compareTo(T var1);
After Java 8,
java.util.function package was introduced which includes some predefined interfaces. We will discuss them one by one below:
Predicate interface is used to check for a boolean value based on our given condition. If the condition satisfied with the given input, it returns true, else false. Predicate accepts an argument T and returns a boolean value.
boolean test(T var);
//This will return true if the passed integer is even Predicate<Integer> p = (i) -> i % 2 == 0;
Function interface is used to perform some operation on the given input and returns some output. Function accepts an argument T and returns R.
R apply(T var)
//This will return the length of passed string argument Function<String,Integer> f = String::length;
Consumer interface is used when we want to accept some input and perform some operation. It does not return anything. Mostly used for printing some content. Consumer accepts an argument T and performs the provided operation without returning anything.
void accept(T var)
//This will print the passed argument 'string' Consumer<String> c = System.out::println;
Supplier interface is used when we want to return some value without giving any inputs. It does not take any arguments and return an object T.
//This will return the system date Supplier<LocalDate> s = () -> LocalDate.now();
A Sample Code for printing the list of students with their roll number, name, marks and grade using the predefined functional interfaces below:
After going through the contents, now you’ll be familiar with functional interfaces in java. Still, if you have any queries, feel free to contact me at email@example.com.
Thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs. Follow me to get updates on different technologies