I have an abstract generic class:
public abstract class AbstractMessageHandler<T extends AbstractMessageHandler>
{
public abstract List<String> getTypesOfMessages();
public abstract void handleMessage(String message, CometClient client);
public T setResponseValues(AbstractMessage request, T response )
{
response.setCompanyId(request.getCompanyId());
response.setMessageGroup(request.getMessageGroup());
response.setUserId(request.getUserId());
response.setTimeStamp(AbstractMessage.getCurrentTimeStamp());
return response;
}
}
I need the generic subclass to be a subclass of this class. In otherwords, the generic must be a subclass of AbstractMessageHandler. This however gives me compilation issues. Can anyone let me know what I am doing wrong?
Thanks
A generic class can extend a non-generic class.
Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class.
Yes, you can define a generic method in a non-generic class in Java.
You need to follow the example of the Enum class:
public abstract class AbstractMessageHandler<T extends AbstractMessageHandler<T>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With