File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generics

 
susan waters
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • 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 ?

}
 
Naseem Khan
Ranch Hand
Posts: 809
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes both are correct.

Naseem
 
Amit Batra
Ranch Hand
Posts: 361
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folowing code works fine.




Thanks & Regards


Naseem
 
Amit Batra
Ranch Hand
Posts: 361
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Pad Ven
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic