Java has always been an Object Oriented programming language, it means we can’t have a function without its class. Other programming languages like C++, PHP, Python, JavaScript and many more, where we write functions and use anywhere, all these languages support functional programming along with Object Oriented programming.

Java introduced Functional interface & Lambda Expressions in Java 8, to leverage functional programming to reduce verbosity.

What is functional Interface?

A Functional Interface is an interface which specifies exactly one abstract method. Its implementation may be treated as Lambda Expression.

Note: An interface is still a functional interface if it has many default methods, as long as it specifies only one abstract method.

Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere

While the main use of Functional interfaces is for lambda expressions, method references, and constructor references, they can still be used, like any interface, with anonymous classes, implemented by classes, or created by factory methods.

Java 8 provides some inbuilt functional interfaces like Comparable, Runnable, Callable etc.

If you want to define any functional interface of your own, you can use annotation @FunctionalInterface for compiler level errors. It will allow to specify only one abstract method.

Example:

@FunctionalInterface
public interface SomeInterface {
public String someMethod(String param);
}

A functional interface is valid even if the @FunctionalInterface annotation would be omitted. It is only for informing the compiler to enforce single abstract method inside interface.

Public interface SomeInterface {
String someMethod(String param);
}

//With default method
@FunctionalInterface
Public interface SomeInterface {
public String someMethod(String param);
default void someDefaultMethod(){
//method Implementation
}

}

// When overriding methods from the java.lang.Object

@FunctionalInterface
Public interface SomeInterface {
public String someMethod(String param);
@Override
public String toString();  //Overridden from Object class

@Override
public boolean equals(Object obj); //Overridden from Object class

}

The signature of abstract method in functional interface is called Functional descriptor because it defines the signature of the lambda expression.

Java 8 has introduced several functional interfaces in java.util.function package which can be used to describe the signature of various lambda expressions.

Here is the complete list of functional interfaces available in java.util.functional:

1. Predicate
2. Consumer
3. Function<T, R>
4. Supplier
5. UnaryOperator
6. BinaryOperator
7. BiPredicate<L, R>
8. BiConsumer<T, U>
9. BiFunction<T, U, R>

Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
RITESH SINGH
RITESH SINGH
5 years ago

very helpful.