• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics Problem

 
Ramesh Sahu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
class test
{

public static void main( String args[] )
{
List<? super String> list = new ArrayList <String> ();
list.add(new Object());
}

}

Compiling the above code produces a compilation error--->
C:\Documents and Settings\174012\java>javac testing.java
testing.java:8: Object() in java.lang.Object cannot be applied to (java.lang.Str
ing)
list.add(new Object("HI"));
^
1 error

In the reference declaration we have used the wildcard which can be of anthing string or it's superclass. Then why the error??
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when there is a ? mark you are not allowed to ADD( you can add only null) or rather MODIFY anything to the reference!! It gives compiler error.
This is a very BASIC rule (of the zillion rules) of generics.
Please read K&B or search for java generics tutorials to know more rules
 
Ramesh Sahu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I am aware of that.
Then why does the following code compile ?

import java.util.*;
class test
{

public static void main( String args[] )
{
List<? super String> list = new ArrayList <Object> ();
list.add(new String("HI"));
}
}
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ramesh Sahu:
Yes I am aware of that.
Then why does the following code compile ?

import java.util.*;
class test
{

public static void main( String args[] )
{
List<? super String> list = new ArrayList <Object> ();
list.add(new String("HI"));
}
}


Because if you use the statament <? super String> means anything in this list CAN BE a String, so no problem on adding it. By the way, its the only case you can add using wildcard.
[ July 29, 2008: Message edited by: Raphael Rabadan ]
 
Anit Nair
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the below statement using wild card ? and super just means the type of collection you can assign to list should String or Parent type of String ex. Object. However if once you assign a collection of type <A> only objects you can add to the list are it are objects A or its sub-types. You cannot added objects that are above A in inheritance hierarchy.
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is another rule!!
when there is a ? mark followed by super it let you add. Thats what i told you, you need to read the tutorials
 
Saurabh Vyas
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


As per K&B book, When you use <? super ... > syntax, you are telling the compiler that you can accept the type on the right-hand side of super or any of its supertypes,and this is the key part.

Now this means any of the below would be true i.e. compile successfully:


If we don't use <? super ..> syntax than following will not compile :


Now coming to the question as to why following code don't compile :


So as per my understanding, This is because, when it comes to adding an object to List with syntax <? super ...> compiler will consider it as follows :
This List


becomes



Thus you can only add an Object of Type String and nothing else. Not even anything of its Super type.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saurabh,
The better way to understand the problem of not adding object of type Object in the below collection

is that if it is allowed to all of super class of String then probably you could have added CharSequence,Serialiable and all the super class in this collection,making the collection hetrogenous which violates the Generics concept.
However null is allowed
 
Mamta Sharma
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raphael,
Can you please make your explanation more clear i am still not able to get it?
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those of you who haven't understood Generics well .... links posted by me over here will help a lot [there are 2 more links in the bottom of the thread]
[ August 01, 2008: Message edited by: Milan Sutaria ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic