- 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.
- package com.instanceofjava.java8;
- /**
- * @author www.Instanceofjava.com
- * @category interview programming questions
- *
- * Description: java 8 functional interfaces
- *
- */
- @FunctionalInterface
- public interface FunctionalInterfaceExample {
- void show();
- }
- 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?
No comments