Would like to get some advise on it
My class structure is like this
public abstract class Base {}
public class Derived1 extends Base {}
public class Derived2 extends Base {}
public class ServiceClass {
public String method1(Derived1 derived1) {
return "derived1";
}
public String method2(Derived2 derived2) {
return "derived2";
}
}
In my main code (MainProg),I am trying to use the same function to refer to the 2 methods in service class and seeing the compilation error as put in the comment
public class MainProg {
public static void main(String[] args) {
ServiceClass serviceClass = new ServiceClass();
//Incompatible types: Base is not convertible to Derived1
Function<? extends Base,String> function1 = serviceClass::method1;
//Incompatible types: Object is not convertible to Derived1
Function<?,String> function2 = serviceClass::method1;
//Incompatible types: Base is not convertible to Derived2
Function<? super Base,String> function3 = serviceClass::method2;
}
}
Is there a way to declare my function object so that same function object can be used to refer to methods taking different type of arguments?