Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Java generics are not covariant.</p> <p>See (<a href="https://stackoverflow.com/search?q=%5Bjava%5D%20collection%20covariance">among many other questions on SO</a>):</p> <ul> <li><a href="https://stackoverflow.com/questions/2660827/java-generics-covariance">java generics covariance</a></li> <li><a href="https://stackoverflow.com/questions/1184295/java-covariance-question">Java covariance</a></li> <li><a href="https://stackoverflow.com/questions/3763192/java-collections-covariance-problem">Java collections covariance problem</a></li> <li><a href="https://stackoverflow.com/questions/6370284/using-generic-collections-in-arguments">Using generic collections in arguments</a></li> </ul> <hr> <p>Solutions:</p> <ul> <li><p>Change the return type of the problematic method. That is, change</p> <pre><code>List&lt;listItem&gt; = (method returning List of type browseItem) // to List&lt;listItem&gt; = (method returning List of type listItem) </code></pre></li> <li><p>Use wildcard covariance (I <em>think</em> that's what this is called):</p> <pre><code>List&lt;? extends listItem&gt; = (method returning List of type browseItem) </code></pre> <p>Be aware that <a href="https://stackoverflow.com/questions/2776975/how-can-i-add-to-list-extends-number-data-structures"><strong>you cannot add items to the list</strong> if you take this route</a>.</p></li> </ul> <hr> <p><sub><strong>N.B.</strong> it is generally good practice to declare list types as <code>List&lt;T&gt;</code> and not <code>ArrayList&lt;T&gt;</code>. The pseudocode above reflects this.</sub></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload