aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic Question in JAVA Beat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic Question in JAVA Beat" Watch "Generic Question in JAVA Beat" New topic
Author

Generic Question in JAVA Beat

Maggie Zhou
Greenhorn

Joined: Sep 09, 2008
Posts: 26
I do not understand the why the answer is call 1,3,4, please help me out, thanks

Q: which can complile without error:


Preparing SCJP 6, try to get high score, and looking for a job...
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

List<? super Object>

means you can add either Object or subclass of Object to list

List<? extends Object>

you can not add anything to list

List<Object>

means you can add either Object or subclass of Object to list

Hope this explains why line 2 won't compile
[ September 09, 2008: Message edited by: ramesh maredu ]

SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Maggie Zhou
Greenhorn

Joined: Sep 09, 2008
Posts: 26
<? super Object> does not mean can add Object or Object's superClass?, I thought the call 1 must be wrong.

So, I just want to double confirm with you: the <? super Object> means you can add either Object or subclass of Object to list?

Then, How about <? super String>, does it have same meaning? or means you can add String(String has no subClass), am I right with this point?
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210



Above code explains ? super behaviour.

regarding String your understanding is correct, we can add only String object as it can not be extended
[ September 09, 2008: Message edited by: ramesh maredu ]
VidyaVikas Yalamanchili
Greenhorn

Joined: Nov 26, 2007
Posts: 10
Hi

<? super Object> means you can have Object type or its Super Class type at type where you are mentioning the type.It means whatever the Type you are mentioning in Generic it should be Object or its Super Object T should be Super of Object mens Super Classtype not SubClass type.But this method will allow you to add thngs to The collection that type.But The method with following Declaration gives compilation error when you try to add the Objects.<? extends Object>.It menas the type can be either Object or its SubClass.Just remeber its Declaration for type only.

When you gave decleration as <? super Object> you need to specify the type in method call.When you specify the the type then you can add only those type objects or the Objects which satisfy IS-A.

I think this will answer your Question.

Please correct me if i am wrong.


with Regards,<br />VidyaVikas Yalamanchili SCJP 5.0
Vierda Mila
Ranch Hand

Joined: Feb 25, 2008
Posts: 61

Then, How about <? super String>, does it have same meaning? or means you can add String(String has no subClass), am I right with this point?


for <? super String>; you can add only String. because String final class and cannot have subclass.
[ September 09, 2008: Message edited by: Vierda Mila ]

SCJP 5
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

for <? super String> you can add String or Object.


this statement is not correct

second line won't compile if you remove comment
Vierda Mila
Ranch Hand

Joined: Feb 25, 2008
Posts: 61
ups..i want to make corection about my statement but you did it early. Thanks Ramesh..

I mean as per below :
List<? super String> a = new ArrayList <String> ();
List<? super String> a = new ArrayList <Object> ();

[ September 09, 2008: Message edited by: Vierda Mila ]
[ September 09, 2008: Message edited by: Vierda Mila ]
Maggie Zhou
Greenhorn

Joined: Sep 09, 2008
Posts: 26
Thanks you all,Now I get it:

When you gave decleration as <? super Object> you need to specify the type in method call.When you specify the the type then you can add only those type objects or the Objects which satisfy IS-A.


so for example:


So this is conclusion! Thanks for help
[ September 09, 2008: Message edited by: Doraemon Zhou ]
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
The way that I like to look at this ( <? super ClassName> ) is like this

if you declare a reference...

List<? super ClassName> list1;

Then that reference can hold a List of ClassName or a List of anything higher in ClassName's class hierarchy.

THAT guarantees that whatever List you assign to list1... it WILL be able to accept a ClassName element.

If list1 will accept a ClassName type, then it will accept a subclass of ClassName type element.


Thus, a List<? super Number> reference will hold a Number, an Integer, a Float, a Double, or any other of the wrapper classes.

---------

Now, if you have a reference typed

List<? extends Number> list2;

then list two will be able to accept a List of Number or anything that is a subclass of Number. That means it will accept List<Number>, List<Integer>, List<Float> or any List of any other wrapper class. With that being true at run-time, the JVM can not be sure what type is currently referenced. All of the "type" information will have been wiped (type erasure). So the compiler, in order to guarantee the type safety of the List, simply will not allow anything to be put on that list using that reference. At run-time, yes, list2 COULD be a reference to List<Number>... but it COULD be a List<Float>. This rule keeps us from adding a short to the List<Float>.


------------------------
Bob
SCJP - 86% - June 11, 2009
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic Question in JAVA Beat