Why do I need a functional interface for working with lambdas?

I think this question is already out there somewhere, but I could not find it.

I do not understand why you need to have a functional interface for working with lambdas. Consider the following example:

public class Test { public static void main(String...args) { TestInterface i = () -> System.out.println("Hans"); // i = (String a) -> System.out.println(a); i.hans(); // i.hans("Hello"); } } public interface TestInterface { public void hans(); // public void hans(String a); } 

This works without problems, but if you uncomment the commented lines, it is not. What for? In my opinion, the compiler should be able to distinguish both methods, since they have different input parameters. Why do I need a functional interface and explode my code?

EDIT: Related duplicates did not answer my question because I am asking about different method parameters. But I got some really useful answers here, thanks to everyone who helped! :)

EDIT2: Sorry, I'm obviously not a native speaker, but to clarify:

 public interface TestInterface { public void hans(); //has no input parameters</br> public void hans(String a); //has 1 input parameter, type String</br> public void hans(String a, int b); //has 2 input parameters, 1. type = String, 2. type = int</br> public void hans(int a, int b); //has also 2 input parameters, but not the same and a different order than `hans(String a, int a);`, so you could distinguish both } public class Test { public static void main(String...args) { TestInterface i = () -> System.out.println("Hans"); i = (String a) -> System.out.println(a); i = (String a, int b) -> System.out.println(a + b); i = (int a, int b) -> System.out.println(a); i.hans(2, 3); //Which method would be called? Of course the one that would take 2 integer arguments. :) } } 

All I ask is about the arguments. The name of the method does not matter, but each method takes a unique order of various arguments, and because of this, Oracle can implement this function, and just make the only method possible for the "Lambda interface".

+17
java lambda java-8 functional-interface
Oct 08 '15 at 8:34
source share
4 answers

When you write:

 TestInterface i = () -> System.out.println("Hans"); 

You give an implementation to the void hans() TestInterface .

If you could assign a lambda expression to an interface that has more than one abstract method (i.e. a non-functional interface), the lambda expression could implement only one of the methods, leaving the other methods unrealized.

You cannot solve this by assigning two lambda expressions with different signatures to the same variable (just as you cannot assign references of two objects to one variable and expect this variable to refer to both objects at the same time) .

+18
Oct 08 '15 at 8:40
source share

The most important reason that they should contain only one method is because confusion is easily possible otherwise. If multiple methods are allowed in an interface, which method should a lambda select if the argument lists are the same?

 interface TestInterface { void first(); void second(); // this is only distinguished from first() by method name String third(); // maybe you could say in this instance "well the return type is different" Object fourth(); // but a String is an Object, too ! } void test() { // which method are you implementing, first or second ? TestInterface a = () -> System.out.println("Ido mein ado mein"); // which method are you implementing, third or fourth ? TestInterface b = () -> return "Ido mein ado mein"; } 
+12
Oct 08 '15 at 8:51
source share

You do not need to create a functional interface to create a lambda function. The interface allows you to create an instance for a future function call.

In your case, you can use the existing Runable interface

Runnable r = () -> System.out.println("Hans");

and then call

r.run();

You can think of lambda -> as just a short hand for:

 Runnable r = new Runnable() { void run() { System.out.println("Hans");` } } 

With lambda, you do not need an anonymous class created under the hood in the above example.

But this has some limitations, in order to figure out which method should be called the interface used with lambdas, it should be SAM (Single Abstract Method). Then we have only one method.

For a more detailed explanation read:

Introduction to functional interfaces - a concept recreated in Java 8

+3
Oct 08 '15 at 8:37
source share

You seem to be looking for anonymous classes . The following code works:

 public class Test { public static void main(String...args) { TestInterface i = new TestInterface() { public void hans() { System.out.println("Hans"); } public void hans(String a) { System.out.println(a); } }; i.hans(); i.hans("Hello"); } } public interface TestInterface { public void hans(); public void hans(String a); } 

Lambda expressions (mostly) are a shorter way to write anonymous classes with just one method. (Similarly, anonymous classes are abbreviated for inner classes, which you use only in one place)

+3
Oct 9 '15 at 11:20
source share



All Articles