• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics question

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serch,

List<List<? extends Shape>> means a List of Lists of objects that extend Shape (which includes Shape and any subclasses).
 
Serch Hdez
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.. and you can't add elements to that list right?..

Regards.
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

  •  
    Ranch Hand
    Posts: 486
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    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..
     
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 43
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 486
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please anyone help to clear this doubt..
    reply
      Bookmark Topic Watch Topic
    • New Topic