• 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

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

void addToGeneric(List<? extends Animal> ani){
// Here I can NOT add anyting to the generic collection since I
// use extends keyword
}

void addToGeneric(List<? super Animal> ani){

// Here I CAN ADD Animal or subtype of Animal (but not super type
// of Animal) to the generic collection right ?

}
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes both are correct.

Naseem
 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im not so sure that may be the correct way to think it.

void addToGeneric(List<? extends Animal> ani){
// Here I can NOT add anyting to the generic collection since I
// use extends keyword
}



The word 'extends' as such is not a problem for adding but the word used with the wildcard '?' will not let you add.


void addToGeneric(List<? super Animal> ani){

// Here I CAN ADD Animal or subtype of Animal (but not super type
// of Animal) to the generic collection right ?
}



Here you can add animal or a SuperType of animal which is probably object assuming you havent derived Animal class from some other class. A subtype of animal like stated in the K&B p597 cant be added.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amitabha Batranab:
Here you can add animal or a SuperType of animal which is probably object assuming you havent derived Animal class from some other class. A subtype of animal like stated in the K&B p597 cant be added.



What you told is for assignment not for adding like this:

List <? super Dog> anim=new ArrayList<Animal>();

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

What you told is for assignment not for adding like this:
List <? super Dog> anim=new ArrayList<Animal>();
Naseem



The above isnt an adding. Its a declaration. By adding I mean using the add() to actually add elements to the collection the legality of which was asked by the original poster.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Folowing code works fine.




Thanks & Regards


Naseem
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can the code above work? If this works then is the Book wrong? Can someone please clarify this? I have an older compiler so i cant try this, but does this result in a runtime exception or something?
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amitabha and Naseem,
Note that in Naseem's code, he is creating a non-type safe list and passing it to the addToGeneric method. That's why it is working, as all type-safety check requirements are not needed anymore. Try giving Animal or Object as in "List <Animal> anim=new ArrayList<Animal>()" and it will compile and work, while using "List <Dog> anim=new ArrayList<Dog>()" to call the addToGeneric obviously results in compile error. So
<? extends type> means cannot ADD anything but can accept that type or its subtypes while <? super type means ADD anything.

Hope this helps. I just completed reading this chapter in K&B but unfortunately lost the plot. This question helped me get back to the chapter and understand the concept.

Regards,
Sathish
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So basically if Icreate an untype safe list as in 'List anim=new ArrayList();' and pass it to a type safe list such as 'static void addToGeneric(List<? super Animal> anim)' I lose all type safetyness? But I thought the compiler would be checking to make sure I dont add the wrong thing to it like a Dog object?
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay I get it now, The type safety is only limited to method parameter, it can dicatet what comes in, but the list itself not being type safe can accept all animals.
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant dictate.
Is the above interpretation right though I still am not 100%?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic