I'm guessing you tried something like:
class MyService implements ContactsService {
@Override
public <T extends Response> T execute(Action<T> action) {
return (T)new GetDetailsResponse();
}
}
The problem with this is that I might have another class MyResponse that implements Response. Then I can call:
Action<MyResponse> action = new Action<MyResponse>();
// you can't actually instantiate an interface, just an example
// the action should be some instance of a class implementing Action<MyResponse>
MyReponse r = myService.execute(action);
But the execute method returns an instance of GetDetailsResponse, which is incompatible with MyReponse. You need to return the type T, which is given by the action you pass to execute.
As far as I can tell you can't instantiate a new variable of type T inside execute (not without some unchecked casts anyway). You probably need the action class to have a way to give you a Response instance that you can return from execute. Something like this:
interface Response {
void setWhatever(String value);
}
interface Action<T extends Response> {
T getResponse();
}
class MyAction implements Action<GetDetailsResponse> {
@Override
public GetDetailsResponse getResponse() {
return new GetDetailsResponse();
}
}
class MyService implements ContactsService {
@Override
public <T extends Response> T execute(Action<T> action) {
T response = action.getResponse();
// do something to Response here like
response.setWhatever("some value");
return response;
}
}