How to extend generic class to be also generic

2020-05-09 12:29发布

问题:

I am having this class : public class My class <T extends Serializable> I want to extend it to a new generic class public class Class2<T> extends My class<T>

It gives bound mismatch error I don't remember the rules now actually and has no access to the material though I got the java see certificate few years ago :(

回答1:

The type parameter T of your sub-class Class2 must satisfy the type bound of the super class Myclass:

public class Class2<T extends Serializable> extends Myclass<T>

Otherwise T cannot be used as the generic type parameter of Myclass.