Functional Interface - Java8

Functional Interface - Java8

Before Java 8, an interface could only declare one or more methods also known as Abstract Method (method with no implementation, just the signature). Starting with Java 8 an interface can also have implementation of one or more methods (knows as Interface Default Method) and static methods along with abstract methods. Interface Default Methods are marked default keyword.

So the question is, what is Functional Interface?

An interface with Single Abstract Method (SAM) is called Functional Interface.

Which means - 
  1. An interface with Single Abstract Method is a Functional Interface
  2. An interface with Single Abstract Method and zero or more default methods and zero or more static method is also a valid Functional Interface.

Below is a functional interface with Single Abstract Method

Below is a functional interface with Single Abstract Method along with default method and static method.

You must have noticed the @FunctionalInterface annotation. What does it mean?

It is optional to annotate a function interface with @FunctionalInterface annotation. The @FunctionalInterface annotation enforces only single abstract method in the interface. If a developer adds additional abstract method in an interface annotated @FunctionalInterface compiler gives error. I recommend using @FunctionalInterface to clearly mark an interface as Functional Interface.

Functional Interface is base of Lambda Expression in Java 8. Lambda Expression can provide implementation of only a Functional Interface. It can not implement any other interface .

Below is Lambda Expression implementation of Greeter functional interface. It is implementing the one and only abstract method of the interface, which takes a String as input and returns a String.
Greeter greeter = o -> "Hi " + o;
Below is Lambda Expression implementation of DefaultGreeter functional interface. It is implementing the one and only abstract method of the interface, which takes a String as input and returns a String. Note that Lambda is using Static method defined in the interface.
DefaultGreeter defaultGreeter = o -> "Hi " + o + " " + DefaultGreeter.suffix();

Here is the Main application class, which is defining Lambda’s and executing them.

Here is output of the main application.


Download Code

You can download complete source code referred in this article with gradle project here. https://github.com/rakeshprajapati1982/functional-interface

Related Articles

Lambda Expression Basics and Syntax - Java8
Lambda Expression v/s Anonymous Inner Class - Java 8

Let me know if you found this blog helpful using checkbox below. Feedback, question or comments are always welcome.

Comments

Other Popular Posts

How to enable JPA eclipselink logging in WAS Liberty

Java Thread Local Storage explained in easiest practical way

EhCache3 as JCache (JSR-107) Implementation with Cache Statistics

Lambda Expression v/s Anonymous Inner Class - Java 8

EhCache3 In Memory Caching for Performance Improvement