3

I was just trying to write a functional interface to understand the different use cases.

Looking at the below code which I have written, I understand that I can have different implementations using lambda expressions. Apart from this can anyone show the complex implementations?

Is it possible to use other default method i.e. addLikeOtherWay in lambda expression? if yes how in my example?

Why would I have interface with only one abstract method? What would be the use case of having only single abstract method in my interface?

public class Calculator {

    public static void main(String[] args) {
        ICalculator i = (int a, int b) -> a + b;
        System.out.println(i.add(5, 2));
        i = (int a, int b) -> a + b + a;
        System.out.println(i.add(5, 2));
    }
}

@FunctionalInterface
interface ICalculator {

    public int add(int a, int b);

    default int addLikeThis(int a, int b) {
        return a + b;
    }

    default int addLikeOtherWay(int a, int b) {
        return a + b + a + b;
    }

}
Naman
  • 27,789
  • 26
  • 218
  • 353
Jigar Naik
  • 1,946
  • 5
  • 29
  • 60
  • The duplicate link does not answer most of my questions – Jigar Naik May 27 '19 at 15:06
  • The goal of the `FunctionalInterface` is to allow you to make lambdas out of it, hence the fact that it's abstract, since the implementation will be the lambda – Yassin Hajaj May 27 '19 at 15:09
  • *Is it possible to use other default method i.e. addLikeOtherWay in lambda expression?* What do you exactly mean by *"in lambda expression"* here? I mean you can call `i.addLikeOtherWay(5, 2)` to invoke the method for sure. – Naman May 27 '19 at 15:18
  • 2
    *Why would I have interface with only one abstract method? What would be the use case of having only single abstract method in my interface?* ... One of the use cases for having a [functional interface is its representation as lambda](https://stackoverflow.com/questions/33010594), apart from which there is always c chance that your interface might not just require being a functional interface which depends on how you design the interface to be. – Naman May 27 '19 at 15:23

2 Answers2

1

"Is it possible to use the default method in a lambda expression?" Yes. In fact, many Functional Interfaces contain default methods. You need one and only one abstract method in an interface for it to be a functional interface, otherwise there would be other interface methods "unimplemented" by the lambda which is not permitted. But here is how the defaults can be applied. The BiFunction interface below was lifted from the API sources, sans JavaDoc.

The following code works because BinaryOperator and UnaryOperator extend BiFunction and Function respectively.

      BinaryOperator<Integer> add = (numb1,numb2)->numb1+numb2;
      UnaryOperator<Integer> mul = resultFromAdd->resultFromAdd*20;
      BinaryOperator<Integer> addThenMul = (numb1,numb2) ->add.andThen(mul).apply(numb1,numb2);
      int result = addThenMul.apply(10,20); // produces (10+20)*20 = 600

The following was lifted form the Java API source files.

       @FunctionalInterface
       public interface BiFunction<T, U, R> {

          R apply(T t, U u);

          default <V> BiFunction<T, U, V> andThen(
                Function<? super R, ? extends V> after) {
             Objects.requireNonNull(after);
             return (T t, U u) -> after.apply(apply(t, u));
          }
       }

In the example code above I could have used BiFunction<Integer,Integer,Integer> and Function<Integer,Integer>. But the *Operator extensions presume the same type for all args so they are easier to use (i.e. less typing).

WJS
  • 36,363
  • 4
  • 24
  • 39
0

Why would I have interface with only one abstract method? What would be the use case of having only single abstract method in my interface?

To facilitate the use of lambda expressions , which are nameless functions. Lambda expressions make code expressive and reduce clutter. It also makes code more readable. This is based my experience working with lambda expressions.

Dishonered
  • 8,449
  • 9
  • 37
  • 50