Results 1 to 5 of 5
  1. #1
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default [Adding with generics]List <? super Cat> list = new ArrayList<Animal>()

    Given that :

    Java Code:
    public class Animal {
    	protected String speak() {
    		return "Animal Speaking";
    	}
    }
    
    class Cat extends Animal {
    	protected String speak() {
    		return "Meaon";
    	}
    }
    If i declare a list such that
    Java Code:
    List<? super Cat> animals = new ArrayList<Animal>();
    , so the created list should be okay with anything that is a parent of Cat.

    I wrote a method to add animals to this class:

    Java Code:
    private static void addAnimal(List<? super Cat> animals) {
    		animals.add(new Cat());
    	}
    This works. however, if i try this:

    Java Code:
    private static void addAnimal(List<? super Cat> animals) {
    		animals.add(new Animal());
    	}
    it fails. Why does it fail? The list declaration says that it can accept anything that is a superclass of Cat. Also, i am trying to use "super" to be able to add to this list(unlike extends).

    Thanks in advance.
    -Gary

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,930
    Rep Power
    22

    Default Re: [Adding with generics]List <? super Cat> list = new ArrayList<Animal>()

    What error does it give?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,436
    Rep Power
    9

    Default Re: [Adding with generics]List <? super Cat> list = new ArrayList<Animal>()

    Perhaps you should examine your class hierarchy a bit more closely.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: [Adding with generics]List <? super Cat> list = new ArrayList<Animal>()

    It says the following :

    (capture#2-of ? super Dog) in the type List<capture#2-of ? super Cat> is not applicable for the arguments (Animal)

    Even in the error message, it says that the type of "super Dog" is not applicable. However, per my understanding , a List<? super Cat> should take Animal (Given Cat extends Animal).

    Not sure what am i missing.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,315
    Blog Entries
    7
    Rep Power
    25

    Default Re: [Adding with generics]List <? super Cat> list = new ArrayList<Animal>()

    Google for "Angelika Langer generics"; she explains it better than I ever could; I too fell in this trap when I first studied those darn generics; its 'super wildcard' construct doesn't do what you think it does.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. List and ArrayList
    By ravian in forum New To Java
    Replies: 3
    Last Post: 07-27-2011, 05:05 PM
  2. Adding List to JTextField
    By nipuna in forum AWT / Swing
    Replies: 7
    Last Post: 09-09-2010, 06:28 AM
  3. trying to set() values of in list of arraylist
    By alvations in forum New To Java
    Replies: 15
    Last Post: 10-13-2008, 09:35 PM
  4. How to access ArrayList in List of List?
    By alvations in forum New To Java
    Replies: 5
    Last Post: 10-08-2008, 12:23 PM
  5. adding a variable in order to a list
    By Jrr in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 02:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •