• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Generics

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have declared a method as shown below.
public static <T> void addandDisp(Collection<T> cs, T t) {
Set s = new HashSet();
for (T o : cs){
s.add(o);
}
for (T o : cs){
System.out.println(o);
}
}

which of the following calls are compiler error free?
//call 1
List<? super String> ls1 = new LinkedList<Object>();
addandDisp(ls1,new String());
//call 2
List<? extends Object> ls2 = new LinkedList<Object>();
addandDisp(ls2,new Object());
//call 3
List<Object> ls3 = new LinkedList<Object>();
addandDisp(ls3,new String());
//call 4
List<? super Object> ls4 = new LinkedList<Object>();
addandDisp(ls4,new Object());

Answers with explanations will be well appreciated!
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets take one by one
// Call 1
List<? super String> ls1 = new LinkedList<Object>();
Here you are defining T as <? super String> which means generic type 'T' is of type String or any super class of String which is Object and hence T is of type String, Object.
addandDisp(ls1, new String(""));
Now ls1 can be assigned to cs of method addandDisp(), Since the generic type T can be String or Object, when you create new String(""), as you know it can be assigned to a reference of String and/or to a reference of type Object and hence this invocation is compiles fine.

// Call 2
List<? extends Object> ls2 = new LinkedList<Object>();
addandDisp(ls2,new Object());

Here you define the generic type T as <? extends Object> which means T can be Object or anything that extends class Object which as you know can be all the known java classes to man-kind. So now you can create a list of any type and assign it to ls2 , say you created a list of type String which can be assigned to ls2 but then the second argument new Object cannot be assigned to T since T is of type String and hence compilation fails here.
To compile the code you will need to change the code as follows
public static <T,T2> void addandDisp(Collection<T> cs, T2 t1) {

//call 3
List<Object> ls3 = new LinkedList<Object>();
addandDisp(ls3,new String());
Here T is of type Object and ls3 can be assinged to first argumnet of method and new String() can again be assigned to second Object reference.

//call 4
List<? super Object> ls4 = new LinkedList<Object>();
addandDisp(ls4,new Object());
Here ? super Object == Object and hence the above explanation is valid.
 
reply
    Bookmark Topic Watch Topic
  • New Topic