Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java 8 functional interface with example

  • Java 8 introduced Lamda expressions and functional interfaces.
  • An interface with one abstract method is known as functional interfaces.
  • Functional interfaces in java 8 marked with @FunctionalInterface.
  • These functional interfaces implementation will be provided by Lamda expressions.
  • Default methods in java 8 interfaces are not abstract methods. But we can define  multiple default methods in functional interface.
  • Java 8 functional interface example program

#1: Java Example program to create java 8 Functional interface.

  1. package com.instanceofjava.java8;

  2. /**
  3.  * @author
  4.  * @category interview programming questions
  5.  * 
  6.  * Description: java 8 functional interfaces
  7.  *
  8.  */
  9. @FunctionalInterface
  10. public interface FunctionalInterfaceExample {

  11. void show();
  12. }

  • Can we create functional interface with @FunctionalInterface annotation without any single abstract method?
  • No. Functional interfaces must have single abstract method inside it.
  • It will throw compile time error : Invalid '@FunctionalInterface' annotation; FunctionalInterfaceExample is not a functional interface.
  • Lets see an example program on this so that we can conclude it.
  • How to use these functional interfaces we will discuss in next post on Lamda expressions.
  • Implement java 8 functional interface using lambda example program

#2: What will happend if we define Functional Interface without a single abstract method in it?

This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

Java 8 functional interface with example


Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription