What does it mean for a non generic class to extend a generic class

后端 未结 6 2064
广开言路
广开言路 2021-02-04 10:23
6条回答
  •  死守一世寂寞
    2021-02-04 10:45

    You are defining a class B that inherits from class C, parameterized with type A. A must be a class or interface.

    E.g.

    class MyStringList extends ArrayList
    

    means that MyString IS AN ArrayList that will only contain String elements. This class could then define e.g. a concatenate() method that returns the concatenation of all Strings in the list.

    Because of this inheritance, you will be able to assign an instance to a List variable:

    List strings = new MyStringList();
    

    But you will not be able to assign it to List type variables with other parameters:

    List objects = new MyStringList(); // does not compile
    
        

    提交回复
    热议问题