CSE 341 -- Java Generics -- Mini-Exercises #2-- Solution

  1. Consider the Wild2.java example. There is a comment that we can't declare the method parameter to be a linked list of Object. (Well, at least with the current main method.) Why not?

    LinkedList<Integer> isn't a subtype of LinkedList<Object>.

  2. Suppose that the compiler let this program through. What could go wrong then?

  3. In the printAll method, since the parameter is a LinkedList<Object>, the method could store a string into it -- even though the actual parameter is LinkedList<Integer>.

  4. Suppose that we did change the method parameter to be a linked list of Object. How could you modify the main method so that this would compile?

  5. Change the two declarations to be LinkedList<Object> in the main method.