41

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Harmeet Singh Taara
  • 6,483
  • 20
  • 73
  • 126
  • 7
    Just as a note: Every interface that has just one abstract method can be seen as **functional interface** and thus be used as one. The annotation is just a hint for programmers that this interface was indeed intended to be used as **functional interface**. Thus it could be possible that you want to design an interface that currently just has one method but you plan to extend it in the future. Then it should not be declared as **functional interface**. Otherwise programs that use this interface for lambda expressions will not be compatible with the new version of the interface anymore . – Zabuzard Aug 05 '17 at 12:41
  • 1
    @Zabuza, this really explains it all. Shame it was not an answer – Akabelle Dec 09 '17 at 21:12

4 Answers4

37

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.

Mark Hurd
  • 10,665
  • 10
  • 68
  • 101
AdityaKeyal
  • 1,208
  • 8
  • 14
  • 1
    Nice link, I also like this one: http://winterbe.com/posts/2014/03/16/java-8-tutorial/ – Gimby Apr 28 '14 at 14:11
  • 2
    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(). – pkalinow Mar 03 '15 at 15:07
14

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.

sn.anurag
  • 617
  • 7
  • 14
  • First of all compiler will give an error if you write this `@FunctionalInterface interface MyInterface { void display(); void display(int x, int y); }` – Vishwa Ratna Mar 11 '19 at 05:29
  • @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. – sn.anurag Mar 11 '19 at 07:59
  • See the answer given by Adityakryal, did you add anything new to it? – Vishwa Ratna Mar 11 '19 at 08:05
  • @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) -> – rakesh kashyap May 20 '19 at 07:27
  • @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. – sn.anurag May 21 '19 at 09:47
5

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.
Sayat Satybald
  • 6,300
  • 5
  • 35
  • 52
0

If you define multiple abstract method in a interface can be possible. But if you define as @FunctionalInterface on the top of interface you will get compile time error "Invalid '@FunctionalInterface' annotation Interf not a functional interface". Even implements is not possible because compile not able guess Type Inference of method(incompatible types Interf is not a functional interface multiple non-overriding abstract methods in interface Interf) Ex1:-

@FunctionalInterface
interface Interf {
   public void m1();
   public void m2();
 }//error:-Invalid '@FunctionalInterface' annotation Interf  not a functional interface

Ex2:-

interface Interf {
   public void m1();
   public void m2();
}
public class abc1 {
    public static void main(String args[]) {
        interf interf = () -> System.out.println("we impl");
        interf.m1();
    }
}//incompatible types Interf is not a functional interface
multiple non-overriding abstract methods in interface Interf
charan
  • 149
  • 5