Why generic type is not applicable for argument extends super class for both?

后端 未结 5 1161
迷失自我
迷失自我 2020-12-03 17:44

Here is the problem that I have been being tried to find the solution.

We have two class definitions. One of two extends other one.

    class T{}         


        
5条回答
  •  醉话见心
    2020-12-03 18:34

    You can create a List list = new ArrayList(); directly, this can allow all subtypes of T into the list. This is actually little difficult to understand. when you declare it as

    List list = ...
    

    It means that it can allow any unknown subtypes of T into the list. But, from that declaration we cannot ensure which is the exact sub-type of T. so, we can only add null into it

提交回复
热议问题