aspose 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......

snehasish barman
Ranch Hand

Joined: Aug 16, 2008
Posts: 30
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 ???


Adobe Certified Expert Flex 4(ACE) (89%), SCJP 5.0 (97%), SCWCD 5.0 (100%), Adobe AIR/Java /J2EE/Spring/Apple iPhone & iPad Application Developer.
~~~ Arise, awake and stop not till the goal is achieved ~~~
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

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 ]

SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
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 ]

If you worry you cannot work... If you work you need not worry
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
What's wrong with option E ?


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
You can add only apple or it's subtypes , but you cannot add it's supertype like Object.
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
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

Joined: Aug 16, 2008
Posts: 30
Thanks all of you for helping me out....now i got the concept....this thread may be closed now....
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generics problem......