Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Doubts

 
Kumaravadivel Subramani
Ranch Hand
Posts: 171
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Can anyone explain me about generics in java? I've quite comfortable with normal generics concept and when the generics with wild character comes I going to hell.
mainly the three topics I've alot of questions after read more articles about this.
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the link Generics
Go through it and then if you have any specific question then ask.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must have to read the chapter 7 of K & B book atleast 2 or 3 times.
Then you will get to understand the generics
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And sorry I didnot noticed earlier

Welcome To JavaRanch......
 
Kumaravadivel Subramani
Ranch Hand
Posts: 171
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for giving a nice document/link to understand basics of generics. But still I've doubts on wild characters with extends and super. Actually what is mean? when and where I need to use these?

Kindly give me a nice simple example of these to understand easily.
 
Gari Jain
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a few such questions in ExamLab on Generics

specially about

i) <? extends B>
ii) <? super Animal>

explaining why we cannot add anything to the i) one
and what can we add to the ii)

You can read the explanation from there.
 
Sumit Khurana
Ranch Hand
Posts: 68
Chrome Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generic is only a compile time concept.....if you are clear with the basics of generic...then think now like you are a compiler.
void foo(List<? extends Animal> list){}

it means you can add anything which passes IS-A relationship with Animal like Cat,Dog.

In this method the compiler only knows about the Animal. Anything which you want, you can only do with the Animal.so,you can add only Animal.

void foo(List<? extends Animal> list){
list.add(new Animal()); //compile time error
}

thing what happens when we pass the Dog list...
List<Dog> d;
foo(d);

then the jvm will throw an exception-ClassCastException.because in Dog you can't add an Animal...so,to avoid these kind of excetion...compiler does not allow you to add anything...

and try to understand super like this
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic