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

Generic Question in JAVA Beat

 
Maggie Zhou
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not understand the why the answer is call 1,3,4, please help me out, thanks

Q: which can complile without error:
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Maggie Zhou
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<? 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
Posts: 210
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vierda Mila
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 320
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic