Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is simplest version of this code with the same problem:</p> <pre><code>import java.util.*; public class GenericTestsClean { public static void main(String[] args) { List&lt;Animal&gt; animalCage = new ArrayList&lt;Animal&gt;(); animalCage.add(new Cat()); animalCage.add(new Dog()); BigCage.printList(animalCage); } } class Animal {} class Dog extends Animal {} class Cat extends Animal {} class BigCage extends Cage { public static &lt;U extends Dog&gt; void printList(List&lt;U&gt; list) { System.out.println("BigCage#printList"); for (Object obj : list) { System.out.println("BigCage: " + obj.getClass().toString()); } } } class Cage { public static void printList(List list) { System.out.println("Cage#printList"); for (Object obj : list) { System.out.println("Cage: " + obj.getClass().toString()); } } } </code></pre> <p>I think that compiller should return error:</p> <pre><code> GenericTestsClean.java:8: &lt;U extends Dog&gt;printList(java.util.List&lt;U&gt;) in BigCage cannot be applied to (java.util.List&lt;Animal&gt;) BigCage.printList(animalCage); ^ 1 error </code></pre> <p>(or sth about name clash with the same errasure) but it doesn't.<br/> After dissasembling (javap -c GenericTestsClean) we got:</p> <pre><code>invokestatic #9; //Method BigCage.printList:(Ljava/util/List;)V </code></pre> <p>Calling <code>java GenericTestsClean</code>:</p> <p><strong>javac 1.6.0_10 version</strong></p> <pre><code>BigCage#printList Exception in thread "main" java.lang.ClassCastException: Cat cannot be cast to Dog at BigCage.printList(GenericTestsClean.java:19) at GenericTestsClean.main(GenericTestsClean.java:8) </code></pre> <p><strong>Eclipse compiller version</strong></p> <pre><code>BigCage#printList BigCage: class Cat BigCage: class Dog </code></pre> <p>IMHO this results are both incorrect.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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