Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java 8 Functional Interfaces

farenda 2017-02-24 0

Java 8 Functional Interfaces is very important concept, because they are heavily used with Lambda Expressions and Streams to simplify code. Let’s take a deep dive!

What is Functional Interface

Every interface that has single abstract method is a Functional Interface in Java 8. They may have static and/or default methods, but have to have only one abstract method (one that doesn’t have any body). This is very important, because the compiler matches Lambda Expressions with this single method to produce fully working objects.

Let’s look at an example of a functional interface with a couple of different methods, but only one abstract method:

package com.farenda.java.lang;

import java.util.Date;

public interface SimpleProcessor {

    // Single Abstract Method:
    int process();

    // Other methods don't metter:
    static Date now() {
        return new Date();
    }

    default String formatDate(Date date) {
        return date.toString();
    }

    default int sum(int a, int b) {
        return a + b;
    }
}

Only functional interfaces can be used as a basis for Lambda Expressions, therefore we can verify whether our interface is functional by creating a Lambda Expression for it:

package com.farenda.java.lang;

public class MyRunner {

    public static void main(String[] args) {
        // SimpleProcessor as Lambda Expression:
        SimpleProcessor processor = () -> 42;

        // The compiler doesn't protest, so let's call it:
        System.out.println("Processing: " + processor.process());
    }
}

The above code prints:

Processing: 42

@FunctionalInterface annotation

To help developers prevent bugs when changing functional interfaces, Java 8 added @FunctionalInterface that tells the compiler to check if the interface has only single abstract method. If not then it won’t compile it.

Here we have a functional interface to which we’ve added a new abstract method:

package com.farenda.java.lang;

import java.util.Date;

@FunctionalInterface
public interface SimpleProcessor {

    int process();

    int postProcess();
}

The compiler immediately sees this and tells us about the error in two ways. The first error is on the @FunctionalInterface annotation itself:

Error:(5, 1) java: Unexpected @FunctionalInterface annotation
com.farenda.java.lang.SimpleProcessor is not a functional interface
  multiple non-overriding abstract methods found in interface
  com.farenda.java.lang.SimpleProcessor

The second error is in Lambdas assigned to the interface (MyRunner above):

Error:(6, 37) java: incompatible types:
   com.farenda.java.lang.SimpleProcessor is not a functional interface
   multiple non-overriding abstract methods found in
   interface com.farenda.java.lang.SimpleProcessor

Notice that @FunctionalInterface annotation is not required to define a functional interface, but it is a good practice, clearly states your intentions, and makes the compiler to immediately notify about errors.

Interfaces with Single Abstract Method, but without the annotation are also functional. They just won’t have compile type check – when you add another abstract method to such interface it will compile, contrary to classes that implement it.

References:

  • Static and default methods in Java 8
  • Lambda Expressions and Streams Tutorial
Share with the World!
Categories Java Tags java, java8
Previous: Java 8 interfaces default and static methods
Next: Static Factory Method pattern

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok