I was learning method reference
topic in java8
. I learnt that method reference provide reference to already existing method (with similar argument types). But I want to know, how is the below code RUNNING successfully even when method arguments are different.
import java.util.function.*;
interface Sayable{
String say();
}
public class Test {
public static void main(String[] args) {
Test test = new Test();
System.out.println(test.doThis(Sayable::say));
}
private <T> T doThis(Function<Sayable,T> func) {
SaySomething ra = new SaySomething();
return func.apply(ra);
}
}
class SaySomething implements Sayable{
@Override
public String say() {
return "hi";
}
}
If I am not wrong here Sayable::say
will get to Function<Sayable,T> func
argument of doThis
method. But say()
method has no argument yet how it gets referenced to R apply(T)
method of Function
interface. If String say()
method is different than R apply(T)
then how is Sayable::say
not showing any error?