This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Doubts 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 Doubts" Watch "Generics Doubts" New topic
Author

Generics Doubts

Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

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.


No pain, No gain.
OCJP 1.6
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
Check the link Generics
Go through it and then if you have any specific question then ask.


Java Programmer | SCJP 1.5 | SCWCD 1.4
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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


SCJP6.0,My blog Ranchers from Delhi
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

And sorry I didnot noticed earlier

Welcome To JavaRanch......
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

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

Joined: Jun 29, 2009
Posts: 100
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.


OCPJP 6-100%; Preparing for GATE11
Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

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
 
wood burning stoves
 
subject: Generics Doubts
 
Similar Threads
generics
Generics question
Generics...
Doubt with generics
Generics