I am practising lambda functions, what I know is to work with them we need to create a functional interface, that contains only one unimplemented abstract method.
So I was wondering why does java don't allow more than one abstract method even if they have different signatures. So I read through related questions on Stack Overflow. And this is what I got. similar question
The answer would be that in order to create a valid implementation, you would need to be able to pass N lambdas at once and this would introduce a lot of ambiguity and a huge decrease in readability.
Now I did a little experiment, I created an interface with two implemented abstract methods with different signatures.
This is myInterface
interface
interface MyInterface {
abstract void func();
abstract void func(int i);
}
And implemented one function as we normally do and the other as a lambda expression
This is Lambda
class
class Lambda implements MyInterface{
public void doWork(MyInterface myInterface) {
myInterface.func();
}
public static void main(String[] args) {
Lambda lambda = new Lambda();
lambda.doWork( e -> System.out.println("func:" + e ));
}
@Override
public void func() {
System.out.println("func");
}
}
Obviously this code doesn't work. I just can't understand even though I have two different signatures and even if one of them is implemented normally then why can't we implemented the other remaining method with a lambda. Is it something to do with code quality and maintainability? Can anyone explain I can't get my head around it? Pls, give an example if possible.