glynbach
glynbach

Reputation: 85

Why a compile type mismatch for ? extends when using an interface

Can someone help me with an explanation as to why this is a type mismatch?

public interface ResponseContainer<T extends ResponsePayload<T>> {

    public T getResponsePayload();

}

public interface ResponsePayload<T> {

    public T getPayload();

}

protected abstract <T> Class<? extends ResponseContainer<? extends ResponsePayload<T>>> getResponseClazz(Class<? extends ResponsePayload<T>> responseClazz);

And to call it:

private void tryThis() {
    ResponseContainer<AccountsResponse> contaner = getResponseClazz(AccountsResponse.class); 
}

Gives the compilation error:

Type mismatch: cannot convert from Class<capture#22-of ? extends ResponseContainer<? extends ResponsePayload<AccountsResponse>>> to ResponseContainer<AccountsResponse>

Is this because I can't syntactically use ? extends T when something is implementing it rather than extending it?

Upvotes: 0

Views: 243

Answers (1)

Stefan Warminski
Stefan Warminski

Reputation: 1835

You need to change the return type of getResponseClazz. You also should change your generic definition to avoid wildcards. The signature should be

protected abstract <T extends ResponsePayload<T>> ResponseContainer<T> getResponseContainer(Class<T> responseClazz);

EDIT Thanks to Thilo, method name changed

Upvotes: 2

Related Questions