Functional Interface Explained in Detail Introduced From Java 8

Originally published August 2020

Functional interfaces are introduced as part of Java 8. It is implemented using the annotation called @FunctionalInterface. It ensures that the interface should have only one abstract method. The usage of the abstract keyword is optional as the method defined inside the interface is by default abstract. It is important to note that a functional interface can have multiple default methods (it can be said concrete methods which are default), but only one abstract method. The default method has been introduced in interface so that a new method can be appended in the class without affecting the implementing class of the existing interfaces. Prior to Java 8, the implementing class of an interface had to implement all the abstract methods defined in the interface.

How to Bind Methods or Constructors to Functional Interfaces

To bind methods or constructors to functional interfaces, we are going to use the FunctionalInterfaceFactory of Burningwave Core library. FunctionalInterfaceFactory component uses to cache all generated functional interfaces for faster access. Before we start coding, we must add the following dependency to our pom.xml:

XML
 




x


 
1
<dependency>
2
    <groupId>org.burningwave</groupId>
3
    <artifactId>core</artifactId>
4
    <version>8.12.6</version>
5
</dependency>



Constructors Binding

To bind a constructors to a functional interface, we will use the following constructor:

The Interface of Java and the Many Faces of It

Before beginning the post of Interface of many faces, have you ever heard a phrase, “Man Of Many Faces”? if not, it means a person can be of everything or anything when it comes to portraying something. It can be one thing and a minute after another without losing its strength.

Java’s interface fits perfectly for this title. It is an integral part of java and has become so powerful over the years, that it shifted the way a Java programmer thinks. It has added various ways of doing things in Java. Depending on the number of methods only, an interface’s definition as well as the object creating process changes.

Writing Functional Interfaces in Java

In this article, we will highlight the purpose and usability of a functional interface in comparison with several alternatives. We will look at how to evolve the code from its basic and rigid implementation to a flexible implementation based on a functional interface. For this, let's consider the following  Melon  class:

Java
 




x
12


 
1
public class Melon {
2
  
3
  private final String type;  
4
  private final int weight;
5
  private final String origin;
6
 
7
  public Melon(String type, int weight, String origin) {
8
    this.type = type;
9
    this.weight = weight;
10
    this.origin = origin;
11
  }
12
  
13
  // getters, toString(), and so on omitted for brevity
14
}



Calling Java Functional Interfaces from Kotlin

Basics

Below is a Functional Interface defined in Java:

Java

Note, that an interface does not need to be annotated with @FunctionalInterface to be treated as one.