0

I am trying to assign a method reference to the following generic interface.

interface Interface1<R> {
  public R doIt();
}

interface Interface2<P, R> {
  public R doIt(P x);
} 

public class Test {
  public static Long foo(Long x) {
      return x;
  }

  public static Long bar() {
      Long x = 1L;
      return x;
  }

  public static void main(String[] args) {
    Interface1<? extends Number> fun1 = Test::bar; //works
    Interface2<? extends Number, ? extends Number> fun2 = Test::foo; // does not work
  }
} 

The second assignment gives me

incompatible types: Number cannot be converted to Long

What am I missing here!

anupamD
  • 862
  • 1
  • 8
  • 21

1 Answers1

0

You cant do this because then you can do fun2.doIt((Number)0.5); The idea is that you need something like Interface2<? super Number, ? extends Number>

Ofek
  • 1,065
  • 6
  • 19