I am trying to have an base implementation of a request as an argument type of an interface’s function.
And then have various classes extend the base request class to give more specific meaning to the request object.
But Java needs the exact class as argument and does not honor classes extended by that base class.
public interface MyInterface{
public String getValue(BaseRequest req);
}
public class MyInterfaceImpl implements MyInterface{
public String getValue(SpecificRequest req){ //Java will give a compile error here.
//Impl
}
}
public interface BaseRequest{
int requiredA;
int requiredB;
//setter and getters for A and B
}
public class SpecificRequest extends BaseRequest{
int specificValC;
//setter and getters for C
}
What is the best way to achieve this pattern? Or I am designing a little too much?
You can achieve something similar using generics.