• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GENERICS

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add only String or subtypes of String.

list.add(new String());
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.


 
Djonatah Stiegler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

<><
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how do I replace



so that it compiles fine?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Djonatah Stiegler
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya now you got 100%.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic