• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics problem......

 
snehasish barman
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is from master exam provided with K&B book....

import java.util.*
class apple {}
class macnitosh extends apple {
public static void main(String[] args) {
List<apple> a=new ArrayList<apple>();
basket(a);
}
// insert code here
}

which inserted at // insert code will complie?(choose all that apply)
A.static void basket(List<? super apple> list) { list.add(new Object());}
B.static void basket(List<? super apple> list) { list.add(new apple());}
C.static void basket(List<apple> list) { list.add(new Object());}
D.static void basket(List<apple> list) { list.add(new apple());}
E.static void basket(List<?> list) { list.add(new apple());}
F.static void basket(List<?> list) { list.size();}

The correct annwers are B,D,F....My question is why option A is incorrect??? The apple list should be able to add anything that comes up in its inheritence tree so adding an Object type must be valid ???
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List<? super apple> list

means list can take any list with super type to apple or list of apple,but you can add only apple object to list.


The apple list should be able to add anything that comes up in its inheritence tree so adding an Object type must be valid ???


No, assume if list receives list of apples,then how adding Object class object to it is possible.
[ September 01, 2008: Message edited by: ramesh maredu ]
 
Siri Naray
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here in option A, List<? super apple> states that you can pass anything that is super to apple AS AN ARGUMENT.
but.. for adding into the list we have to notice the
List<apple> a=new ArrayList<apple>() declaration which says you can add only list of TYPE APPLE. since we are passing 'a' as an argument to the basket(List<? super apple> list), the add is trying to add the Object type to a List 'a' that can take only apple type.
[ September 02, 2008: Message edited by: Sirisha Ghatty ]
 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with option E ?
 
Nabila Mohammad
Ranch Hand
Posts: 661
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add only apple or it's subtypes , but you cannot add it's supertype like Object.
 
Siri Naray
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thr option E is incorrect because when you use "?" or "? extends" we cannot add anything to the list and when we try to add we get a compiler error.
Refer page 594 of K&B last paragraph to see a clear explanation.
[ September 02, 2008: Message edited by: Sirisha Ghatty ]
 
snehasish barman
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all of you for helping me out....now i got the concept....this thread may be closed now....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic