- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the rules for a functional interface in Java?
A functional interface is a special kind of interface with exactly one abstract method in which lambda expression parameters and return types are matched. It provides target types for lambda expressions and method references.
Rules for a functional interface
- A functional interface must have exactly one abstract method.
- A functional interface has any number of default methods because they are not abstract and implementation already provided by the same.
- A functional interface declares an abstract method overriding one of the public methods from java.lang.Object still considered as functional interface. The reason is any implementation class to this interface can have implementation for this abstract method either from a superclass or defined by the implementation class itself.
Syntax
@FunctionalInterface interface <interface-name> { // only one abstract method // static or default methods }
Example
import java.util.Date; @FunctionalInterface interface DateFunction { int process(); static Date now() { return new Date(); } default String formatDate(Date date) { return date.toString(); } default int sum(int a, int b) { return a + b; } } public class LambdaFunctionalInterfaceTest { public static void main(String[] args) { DateFunction dateFunc = () -> 77; // lambda expression System.out.println(dateFunc.process()); } }
Output
77
- Related Articles
- What are the rules for the Subscriber interface in Java 9?
- What are the rules for the Subscription interface in Java 9?
- What are the rules for the Publisher interface in Java 9?
- What are the rules for private methods in an interface in Java 9?
- What is a functional interface in Java?
- What is the generic functional interface in Java?
- What is Functional Interface in Java 8?
- Explain the inference rules for functional dependencies in DBMS
- What are the rules for a local variable in lambda expression in Java?
- What are the rules for formal parameters in a lambda expression in Java?
- What are the scoping rules for lambda expressions in Java?\n
- What are the rules for the body of lambda expression in Java?
- What are the rules for external declarations in JShell in Java 9?
- What are the modifiers allowed for methods in an Interface in java?
- What are the in-built functional interfaces in Java?

Advertisements