what is functional interface in java

1 year ago 70
Nature

A functional interface in Java is an interface that contains only one abstract method. They can have any number of default or static methods, but they must contain only one abstract method. Functional interfaces are also known as Single Abstract Method (SAM) interfaces. Java 8 introduced functional interfaces to enable users to implement functional programming in Java. Lambda expressions can be used to represent the instance of a functional interface. Lambda expressions are anonymous functions that can take parameters and return a value, and they allow us to use a lambda expression to initiate the interfaces method and avoid using lengthy code. It is recommended that all functional interfaces have an informative @FunctionalInterface annotation, which communicates the purpose of the interface and allows a compiler to generate an error if the annotated interface does not satisfy the conditions. Some examples of functional interfaces in Java include Runnable, ActionListener, and Comparable. Functional interfaces are mainly of four types: Consumer, Predicate, Function, and Supplier.