File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Problem 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 "Generics Problem" Watch "Generics Problem" New topic
Author

Generics Problem

Ramesh Sahu
Ranch Hand

Joined: Jul 14, 2008
Posts: 32
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

Joined: Jul 10, 2008
Posts: 118
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


SCJP 6 83%
Working on SCWCD/OCPJWCD
Ramesh Sahu
Ranch Hand

Joined: Jul 14, 2008
Posts: 32
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

Joined: Jul 05, 2008
Posts: 141
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 ]

SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Anit Nair
Greenhorn

Joined: May 09, 2008
Posts: 12
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

Joined: Jul 10, 2008
Posts: 118
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

Joined: Sep 02, 2003
Posts: 72


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

Joined: Nov 01, 2006
Posts: 381
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


~Sanjeev Singh<br />SCJP 1.5
Mamta Sharma
Greenhorn

Joined: Jun 03, 2008
Posts: 25
Hi Raphael,
Can you please make your explanation more clear i am still not able to get it?
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics Problem
 
Similar Threads
one doubt in list
A Generic Question
GENERICS
Genrecis and Wildcard
one more doubt abt generics