» » » 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 www.Instanceofjava.com
  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?

functional interface example in java 8

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu