Why Functional Interfaces in Java 8 have one Abstract Method?

22,844

Solution 1

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.

Solution 2

If Java would have allowed having two abstract methods, then lambda expression would be required to provide an implementation of both the methods. Because, calling method won't know, which method to call out of those 2 abstract methods. It could have called the one which is not implemented. For example

If Java would have allowed this kind of functional interface

@FunctionalInterface
interface MyInterface {
    void display();
    void display(int x, int y);
}

Then on implementing the following would have not been possible.

public class LambdaExpression {
    public static void main(String[] args) {
        MyInterface ref = () -> System.out.print("It is display from sout");
        ref.display(2, 3);

    }
}

Since display(int x, int y) is not implemented, it will give the error. That is why the functional interface is a single abstract method interface.

Solution 3

Functional Interface lets us call an object as if it were a function, which lets us pass verbs(functions) around our program rather than nouns(objects). Implementations of Functional Interface perform a single well-defined action, as any method should, with a name like run, execute, perform, apply, or some other generic verb.[1]

  1. Functional Programming Patterns in Scala and Clojure.
Share:
22,844
Harmeet Singh Taara
Author by

Harmeet Singh Taara

Harmeet starts his career from Java EE development and building applications using Java frameworks. He is the die-hard fan of technology and exploring new approach, frameworks, methodology and more. With some industrial experience, he gains some knowledge about Scala and reactive applications and loves Lightbend technology stack. That's why he joins Knoldus. In knoldus start building application using Java 8 and Scala and other Lightbend technologies like Akka, Play, Lagom and more.

Updated on July 12, 2021

Comments

  • Harmeet Singh Taara
    Harmeet Singh Taara almost 3 years

    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?

  • Gimby
    Gimby about 10 years
    Nice link, I also like this one: winterbe.com/posts/2014/03/16/java-8-tutorial
  • pkalinow
    pkalinow about 9 years
    Actually, there is one exception to the rule that only one abstract method is allowed - a functional interface can have another abstract methods if they are implemented by java.lang.Object, for example toString().
  • Vishwa Ratna
    Vishwa Ratna about 5 years
    First of all compiler will give an error if you write this @FunctionalInterface interface MyInterface { void display(); void display(int x, int y); }
  • sn.anurag
    sn.anurag about 5 years
    @CommonMan That is what I have answered - why Java has not allowed it. And, I did not find the above answer explanatory enough. That's why only have posted it.
  • Vishwa Ratna
    Vishwa Ratna about 5 years
    See the answer given by Adityakryal, did you add anything new to it?
  • rakesh kashyap
    rakesh kashyap almost 5 years
    @sn.anurag i still cannot wrap my head around this. when I have different method signatures, why can't compiler differentiate between () -> and (x,y) ->
  • sn.anurag
    sn.anurag almost 5 years
    @rakeshkashyap compiler can differentiate but you are suppose to provide implementation to both methods in your instance. And, you won't be able to do that. For one instance, you can provide implementation to at most one method using lambda expression.