title: adding subClass object to List<? extends Subclass> create error
body: <p>I have a problem with generic List. I spent few hours searching for a solution and the best I could do is below. Here is my code:</p>
  
  <pre><code>private &lt;T extends SonarContainPaging&gt; T getSonarObjectFromPage(String url, Map&lt;String, Object&gt; uriVariables, Class&lt;T&gt; klass, int page) {
      String json = //create the Json
  
      return objectMappingFromJson(json, klass);
  }
  
  private &lt;T extends SonarContainPaging&gt; void getSonarListContainingPaging(String url, Map&lt;String, Object&gt; uriVariables, Class&lt;T&gt; klass, List&lt;T&gt; list) {
      int page = 1;
  
      SonarContainPaging sonarObject = getSonarObjectFromPage(url, uriVariables, klass, page);
      page++;
  
      list.add(sonarObject);
      while (sonarObject.canContinueLooping(page)) {
          sonarObject = getSonarObjectFromPage(url, uriVariables, klass, page);
          page++;
  
          list.add(sonarObject);
      }
  }
  </code></pre>
  
  <p>the method <code>objectMappingFromJson</code> call <code>gson.fromJson(json, classOfObject);</code>
  and return a generic object <code>T</code>.</p>
  
  <p>When I tried to add the object <code>sonarObject</code> in the list, I get the following error:</p>
  
  <blockquote>
    <p>The method add(T) in the type List is not applicable for the arguments (SonarContainPaging)</p>
  </blockquote>
  
  <p>I think casting the <code>sonarObject</code> to the type <code>(T)</code> would solve the problem. But it is strange. <br>
  I declared that <code>&lt;T extends SonarContainPaging&gt;</code>, I don't get why there is this error. <br></p>
  
  <hr>
  
  <p><strong>EDIT:</strong>
  Like I was told by @Andy Turner, I changed the method to:</p>
  
  <pre><code>private &lt;T extends SonarContainPaging&gt; void getSonarListContainingPaging(String url, Map&lt;String, Object&gt; uriVariables, Class&lt;T&gt; klass,
                                                                           List&lt;? super SonarContainPaging&gt; list);
  </code></pre>
  
  <p>Which solve the problem of adding an object in the list.
  But it created an another one:</p>
  
  <pre><code>List&lt;SonarComponentTree&gt; list = new ArrayList&lt;SonarComponentTree&gt;();
  getSonarListContainingPaging(urlBuilder.toString(), uriVariables, SonarComponentTree.class, list);
  </code></pre>
  
  <p>I cannot call the method <code>getSonarListContainingPaging</code> with a List of <code>SonarComponentTree</code>.</p>
  
  <blockquote>
    <p><code>SonarComponentTree</code> implements <code>SonarContainPaging</code>.</p>
  </blockquote>
  
  <pre><code>The method getSonarListContainingPaging(String, Map&lt;String,Object&gt;, Class&lt;T&gt;, List&lt;? super SonarContainPaging&gt;)
  in the type SonarRestApiServiceImpl is not applicable for the arguments
  (String, Map&lt;String,Object&gt;, Class&lt;SonarComponentTree&gt;, List&lt;SonarComponentTree&gt;)
  </code></pre>
  
  <p>If I change back the list parameter to <code>List&lt;? extends SonarContainPaging&gt; list</code> the issue is resolved, but I am back where I was at the beginning.<br>
  I can't add an another object in the list.</p>
  
  <p>I read @Michael Myers solution in the link given by @Andy Turner and it seems that I should use neither <code>extends</code> nor <code>super</code>, actually, it may be a conception error.</p>
  
  <hr>
  
  <p><strong>Final Edit:</strong></p>
  
  <p>The first answer actually works, you just need to cast the object to <code>(T)sonarObject</code>.
  You just need to bear the warning.</p>