wood burning stoves 2.0
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generics wildcard" Watch "generics wildcard" New topic

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.

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"));
public static void take(List<? extends Number>x){
//In this method nothing can be added to List x
//This method becomes read only method
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){
I agree. Here's the link: http://aspose.com/file-tools
subject: generics wildcard
It's not a secret anymore!