Functional Interfaces in Java 8

Functional Interfaces in Java 8

This post works as an introduction to the Functional Interfaces in Java 8. We will be covering some of the basics of the Functional Interfaces introduced in Java 8.

 

Introduction

Java has always been called as OOP (Object Oriented Programming) language. Functions do not exist independently in Java and everything is part of Class and use to class/object to invoke any function (ie, JNI). Functional Programming Language is written independent functions and uses them when required such as C++, JavaScript.

Function Interface was introduced in Java8 as new features along with Stream API, Time API, Lambda Expression, etc., Function Interface having only one abstract method which means interface contains only one abstract method and it can have multiple default methods.

 

1.FunctionalInterface Example 

@FunctionalInterface annotation was introduced along with other annotation in Java 8 which can be used to validate at the compile time that can’t have more than one method beside default.

Let’s create a simple example to understand few details of the @FunctionalInterface.

@FunctionalInterface
public interface Account {

    int returns();

    default void printDetails() {
        System.out.println("Default Methods");
    }
}

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 (ie., abstract methods that override the method in the object).

@FunctionalInterface
public interface Account {

    int returns();

    default void printDetails() {
        System.out.println("Default Methods");
    }

    /*
     * Overridden from Object class
     */
    public String toString();

    public int hashCode();

}

FunctionalInterface annotation is not mandatory to use because of the compiler will treat any interface meeting the definition of a functional interface as a functional interface regardless of whether or not a FunctionalInterface annotation is present on the interface declaration.

Java earlier version having a single abstract method in the interface and they were known as simple abstract method interface e.g.,Runnable Interface (Run method), Comparator Interface.

 

2.Functional Interfaces Type 

The declaration of a functional interface allows a functional interface type to be used in a program by the developer and these are segregated as four kinds of functional interface type based on a declaration.

 

Another interesting part is Interface can extend another interface and In this scenario, Interface extending in functional and it doesn’t declare any new abstract methods then the new interface is functional which will satisfy the definition.

public interface Bank {

    public void openAccount();
}

Account interface extends Bank interface which has only one OpenAccount method and Account interface doesn’t have any abstract method but still, it is valid functional Interface.

@FunctionalInterface
public interface Account extends  Bank {}

Let see one more example, consider while inherits two abstract which have the same signature.

public interface SocialMedia {
  public void OpenAccount();
}

In this case, still, the inherited methods logically represent a signal method so Account is a function interface.

@FunctionalInterface
public interface Account extends SocialMedia {
}

 

Consider above example, if one is return type substitutable then Account causes compile time error because it’s inheriting two methods.

 

interface Bank {public void OpenAccount();}
interface SocialMedia {public boolean OpenAccount();}

There are numbers of functional interfaces defined under java.util.function package, some of the useful interfaces are

  • Consumer
  • Function
  • Supplier
  • Predicate

 

2.1  Predicate Example

import java.util.function.Predicate;;

/**
 * @author Arunchandu
 */
public class PredicateTestExample {

    public static void main(String[] args) {

        Predicate<String> i = (name) -> name.contains("java");
        System.out.println(i.test("Thank you for Learning from javadevjournal.com"));
    }

}

 

2.2  Pass Function Example

import java.util.function.Function;

public class PassFunction {

    public static void main(String[] args) {
        int percentage = 5;
        String result = calc((amount) -> "Tax : " + ((amount * 100) / percentage), 10);

        System.out.println(result);

    }

    public static String calc(Function<Float, String> bi, float i) {
        return bi.apply(i);
    }

}

 

Summary

In this post, we got an introduction to the Functional Interfaces in Java 8. We covered some of the aspects of functional interfaces along with few interesting functional interfaces introduced in Java 8.

If you are starting with Java 8, read our following articles on Java 8

  1. Introduction to Java 8 Streams
  2. java 8 optional
  3. Java 8 StringJoiner

Arunchandu Ganta

Arunchandu Ganta has around 5+ years of experience in the IT Industry and worked on developing complicated Business Systems and Web Applications on the J2EE platform. Have experience in all phases of the System Development Life Cycle (SDLC) with an emphasis on implementation, integration and testing, deployment, and maintenance. Extensively involved in Designing, implementing solutions that support business goals in collaboration with senior management and stakeholders.

You can connect with Arunchandu on Connect with Arunchandu

Leave a Reply

1 Comment on "Functional Interfaces in Java 8"

Notify of
avatar
Sort by:   newest | oldest | most voted
trackback

[…] For more information, read our post on Functional interfaces in Java. […]

wpDiscuz