- Functional interfaces will contains single abstract method with @FunctionalInterface annotation.
- Check below two posts for defining functional interface and implementing functional interface method using Lamda in java 8.
- Java 8 functional interface with example
- Implement java 8 functional interface using lambda example program
- Now the question is can we declare or define multiple abstract methods in one functional interface in java 8.
- No. Functional interface should contain only one abstract method so that Lamda will implement it.
- Lets see what happens if we define multiple abstract methods inside a functional interface
- Note: all methods inside any interface by default abstract methods.
- package com.instanceofjava.java8;
- /**
- * @author www.Instanceofjava.com
- * @category interview programming questions
- *
- * Description: java 8 functional interfaces
- *
- */
- @FunctionalInterface
- public interface FunctionalInterfaceExample {
- //compile time error: Invalid '@FunctionalInterface' annotation; FunctionalInterfaceExample is
- not a functional interface
- void show();
- void calculate();
- }
- If we define multiple abstract methods inside a functional interface it will throw a compile time error.
- Invalid '@FunctionalInterface' annotation; FunctionalInterfaceExample is not a functional interface
No comments