Add Strings through use of generic 'extends' causes compiler error

后端 未结 3 1754
滥情空心
滥情空心 2020-12-02 03:20

Below code :

List genericNames = new ArrayList();
genericNames.add(\"John\");

Gives compiler error :

3条回答
  •  春和景丽
    2020-12-02 03:54

    String is a final class and cannot be extended. Additionally, for the case you seem to be interested in, you do not need the extends keyword. List will do what you seem to want. That will allow Strings and sub-classes of String (if such a thing could exist, which it can't since String is final).

提交回复
热议问题