| Author |
GENERICS
|
Abhi vijay
Ranch Hand
Joined: Sep 16, 2008
Posts: 509
|
|
Source: Inquisition
Here according to me Line2 should compile and run.
But the answer says. Compile time error.
But <? super String> means anything that is a super class of String can be added into the collection, Object is a superclass of String?
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
You can add only String or subtypes of String.
list.add(new String());
|
SCJP 6
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9191
|
|
Abhi vijay wrote:But <? super String> means anything that is a super class of String can be added into the collection, Object is a superclass of String?
No. ? super String means that the List can be of any super type of String. It might be CharSequence, Serializable, Comparable etc or even String itself. So you are not sure about which type it is. But one thing is sure. The type is a super type of String. So you can store objects of String and it's sub-classes into the list. Since String is a final class, so you can store only String class's objects in the list...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
Abhi vijay wrote:
But <? super String> means anything that is a super class of String can be added into the collection, Object is a superclass of String?
This means List reference can take any list reference that is String or SuperClass of String.
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
list parameterized reference variable can refer to object parameterized with either <String> or Super class of String.
super just gives you flexibility of adding objects to it unlike extends where there is NO NO to add.
|
cmbhatt
|
 |
Djonatah Stiegler
Ranch Hand
Joined: Oct 30, 2008
Posts: 32
|
|
Abhi vijay wrote:Source: Inquisition
Here according to me Line2 should compile and run.
But the answer says. Compile time error.
But <? super String> means anything that is a super class of String can be added into the collection, Object is a superclass of String?
You are wrong.
This means the List can accept only collections of string super types, or strings. It is not about the elements that will be inserted into the collection.
<><
|
From Brazil
|
 |
Abhi vijay
Ranch Hand
Joined: Sep 16, 2008
Posts: 509
|
|
So how do I replace
so that it compiles fine?
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
|
|
 |
Djonatah Stiegler
Ranch Hand
Joined: Oct 30, 2008
Posts: 32
|
|
It 's going to compile fine when you have a collection that stores Objects
like that:
or that
But since Object class doens't have a super type it is useless...
<><
|
 |
Abhi vijay
Ranch Hand
Joined: Sep 16, 2008
Posts: 509
|
|
No, I mean
I mean Line 2 can be replaced with list.add(new String()); what else can it be replaced with?
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
By subchilds of String, but String is a final class so it cannot be replaced with anything else.
Ya but you can replace with this:
|
 |
Abhi vijay
Ranch Hand
Joined: Sep 16, 2008
Posts: 509
|
|
Thanks, a lot guys!!
Now its clear, actually I was trying to mix up LINE1 AND LINE2.
In line1,
List<? super String> list = new ArrayList<Object>(); // Line1.
Here on the Right hand Side , we can declare either String or any supertype of String, namely String,Serializable, CharSequence, Comparable<String>.
Whereas in Line2,
we can add only String or subtypes of String.
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
|
Ya now you got 100%.
|
 |
 |
|
|
subject: GENERICS
|
|
|