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

后端 未结 5 1157
迷失自我
迷失自我 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:32

    There are boundary rules defined for Java Generics when using WildCards

      **extends Wildcard Boundary**
    

    List means a List of objects that are instances of the class T, or subclasses of T (e.g. TT). This means a Read is fine , but insertion would fail as you dont know whether the class is Typed to T

    **super Wildcard Boundary**
    

    When you know that the list is typed to either T, or a superclass of T, it is safe to insert instances of T or subclasses of T (e.g.TT ) into the list.

    In your example , you should use "super"

提交回复
热议问题