1

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.

Lovesh Dongre
  • 1,294
  • 8
  • 23
  • 3
    Using `public` and `abstract` for methods in an interface is unnecessary, and therefor recommended to avoid. The lambda you pass in your `main` method has no relation the `Lambda` class at all, so your question doesn't make much sense. And in any case, the specification says a functional interface cannot have more than one abstract method. So an interface with two abstract methods cannot be used by specification. – Mark Rotteveel Jul 10 '20 at 19:15

1 Answers1

0

You can create a sub-interface of MyInterface with all but one methods implemented using the default keyword. It allows you to provide default implementations that interface implementers "inherit" if they do not provide own implementations. Thus methods with default implementation are not abstract anymore. If you default-implement all but one interface in s sub-interface effectively you end up with a functional interface (a single abstract method interface). And this allows you to use lambda expressions.

interface PartialInterface extends MyInterface {
    @Override
    default void func() {
        System.out.println("func");
    }
}
class Lambda {
    public void doWork(PartialInterface myInterface) {
        // both methods from the interface can be used
        myInterface.func();    // the default implementation "inherited" from PartialInterface
        myInterface.func(42);  // the single abstract method (inherited by PartialInterface from myInterface) overridden by the lambda expression
    }

    public static void main(String[] args) {
        Lambda lambda = new Lambda();
        lambda.doWork(e -> System.out.println("func:" + e));
    }
}
tastaturtier
  • 126
  • 1
  • 5