Reputation: 858
I want to have a method signature that looks as such:
public <? extends IDto> convertToResponseDto();
I came up with this from a valid signature that looks like:
public List<? extends IDto> convertToResponseDto();
As it turns out Type <? extends IDto>
is not a valid return type. I feel like the intention of what I am trying to return is quite clear, but not sure what the correct syntax is
EDIT
This question was answered by @ErwinBolwidt, but gives a new problem in a use case. I have a case where I am using generics:
protected final <E extends IDto> E findOneInternal(final Long id) {
return getService().findOne(id).convertToResponseDto();
}
In this case, convertToResponseDto() returns IDto, and not the concrete class, as its not known at this pint because it gets the service of type T
Upvotes: 0
Views: 106
Reputation: 31299
You can override an interface method or superclass method with a more specific subtype as the return type. Say your superclass says
public IDto convertToDTO()
Then your subclass can say
public MySpecificDTO convertToDTO()
That's implied by the Java language and doesn't need any generics.
Upvotes: 2
Reputation: 1
You can define a generic type
public <T extends IDto> T convertToResponseDto();
Upvotes: 0