Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Functional Interfaces in Java 8 have one Abstract Method?

As we know in Java 8, the concept of functional interfaces are introduced. A Functional Interface has one abstract method and several default or static methods are possible.

But why should a Functional interface have only one abstract method? If Interface has more then one abstract method, why is this not a Functional Interface?

like image 653
Harmeet Singh Taara Avatar asked Apr 28 '14 13:04

Harmeet Singh Taara


People also ask

Can a functional interface have multiple abstract methods?

A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit.

What happens if we have more than one abstract method in functional interface?

The @FunctionalInterface Annotation When this annotation is specified on an interface, a compilation error will occur if the interface has more than one abstract method.

Can functional interface have no abstract methods?

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.

Is functional interface is abstract class in java 8?

Java 8 Functional Interfaces. A functional interface is an interface that has only one abstract method. It can contain any number of default and static methods but the abstract method it contains is exactly one.


1 Answers

The functional interface also known as Single Abstract Method Interface was introduced to facilitate Lambda functions. Since a lambda function can only provide the implementation for 1 method it is mandatory for the functional interface to have ONLY one abstract method. For more details refer here.

Edit -> Also worth noting here is that, a functional interface can have a default implementation in the interface. You will find a lot more details on the implementation on the link above.

like image 96
AdityaKeyal Avatar answered Sep 21 '22 12:09

AdityaKeyal