This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generics wildcard Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generics wildcard" Watch "generics wildcard" New topic
Author

generics wildcard

ming ming
Ranch Hand

Joined: May 17, 2006
Posts: 54
in the following code what can we add ?
babu sharath
Ranch Hand

Joined: Jul 06, 2006
Posts: 42
If there is wild card , We can not add any thing .
( unless it is super .. Ex: ? extends super )
babu sharath
Ranch Hand

Joined: Jul 06, 2006
Posts: 42
Sorry ... In previous reply example was wrong...
It is like .. ( ? super Number )
than we can add .
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
Let's take into consideration, the following class hierarchy.


When we say, ? extends A, it means that both B and A can also be taken into consideration. Now if we have,


and, say, add() was possible, then we would be able to add an element of type C also. Just to dis-allow this, its a compile time error.

regards,
vijay.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
Check code below


import java.util.*;
public class Test {
public static void main (String args []) {
List<Number>x=new ArrayList<Number>();
x.add(new Integer("10"));
take(x);
take1(x);
}
public static void take(List<? extends Number>x){
//In this method nothing can be added to List x
//This method becomes read only method
System.out.println(x.get(0));
}
public static void take1(List<? super Number>x){
//In this method anything extends Number can
//be added to List x
x.add(new Integer("20"));
x.add(new Long("20"));
x.add(new Float("20.120"));
for(Object n:x){
System.out.println(n);
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generics wildcard
 
Similar Threads
Generic method add query
Hate to drag up old woes but..
generics
Generics Lower and Upper Bound
Generics