wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics question" Watch "Generics question" New topic
Author

Generics question

Serch Hdez
Ranch Hand

Joined: May 01, 2008
Posts: 43
Hi there in the tutorial of sun at: http://java.sun.com/docs/books/tutorial/extra/generics/methods.html about Generics there is the following coding snipet:

static List<List<? extends Shape>> history =
new ArrayList<List<? extends Shape>>();
public void drawAll(List<? extends Shape> shapes) {
history.addLast(shapes);
for (Shape s: shapes) {
s.draw(this);
}
}

I don't understand what does List<List ? extends Shape>> means? can somebody help thanks a lot..
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Serch,

List<List<? extends Shape>> means a List of Lists of objects that extend Shape (which includes Shape and any subclasses).


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Serch Hdez
Ranch Hand

Joined: May 01, 2008
Posts: 43
Thank you.. and you can't add elements to that list right?..

Regards.
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Originally posted by Serch Hdez:
Thank you.. and you can't add elements to that list right?..

Regards.


Correct. As stated in the Get and Put Principle (Naftalin and Wadler):
  • use an extends wildcard when you only get values out of a structure,
  • use a super wildcard when you only put values into a structure,
  • don't use a wildcard when you both get and put.

  • Dinesh Tahiliani
    Ranch Hand

    Joined: Aug 06, 2007
    Posts: 486


    As far as know, there is only one wildcard which is
    ?
    .
    what is super wildcard ?

    Can you please shed some lights on this please in detail..


    Thanks<br />Dinesh
    Rose Thomas
    Greenhorn

    Joined: Dec 06, 2006
    Posts: 8
    Originally posted by Dinesh Tahiliani:


    As far as know, there is only one wildcard which is .
    what is super wildcard ?

    Can you please shed some lights on this please in detail..




    wildcard super means accepting generic type that is of the type defined in super tag, or a supertype of type. Nothing lower in inheritance tree can come in.

    e.g. List<? super Dog> animal will accept type Dog OR supertype of Dog(say Animal or even Object). But not "fluffy" for eg.
    Serch Hdez
    Ranch Hand

    Joined: May 01, 2008
    Posts: 43
    Originally posted by Rose Thomas:




    wildcard super means accepting generic type that is of the type defined in super tag, or a supertype of type. Nothing lower in inheritance tree can come in.

    e.g. List<? super Dog> animal will accept type Dog OR supertype of Dog(say Animal or even Object). But not "fluffy" for eg.


    Hi Rose,
    I have the following code:

    package chapter7.collections;

    import java.util.*;

    public class ListGen {


    public static void main(String[] args) {

    List<? super String> list = new ArrayList<String>();
    list.add("nothing");
    list.add(new Object());
    Bar b = new Bar();
    b.addToList(list);
    System.out.println("it all came good");
    }

    }

    class Bar {

    public void addToList(List<Object> list){

    list.add(new Dog());

    }

    class Dog{

    int bark;

    public void eat(){}
    }
    }

    When I try to add the object in the array list I'm getting a compiler error, I'm using eclipse and it says that line is invalid along with the call to bar.addToList() and tried to instantiate the arrarlist as List<? super String> list = new ArrayList<Object>(); and it still gives me errors why I cannot add an object since its a super class of string?... thanks.
    Dinesh Tahiliani
    Ranch Hand

    Joined: Aug 06, 2007
    Posts: 486
    Please anyone help to clear this doubt..
     
     
    subject: Generics question
     
    Similar Threads
    Generics
    Collections class ( Generics )
    Methods and Generics
    Generics: Shape example does not work as SUN says
    generics