A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Programmer Certification (SCJP/OCPJP)
Doubt in a question of Generics
Bijendra S. Rajput
Joined: Sep 19, 2006
Feb 21, 2007 06:03:00
I took this question from javabeat site.
Which ones of the following lines can be compiled without an error?
a)Basket b = new Basket();
b)Basket b1 = new Basket<Fruit>();
c)Basket<Fruit> b2 = new Basket<Fruit>();
d)Basket<Apple> b3 = new Basket<Fruit>();
e)Basket<Fruit> b4 = new Basket<Apple>();
f)Basket<?> b5 = new Basket<Apple>();
g)Basket<Apple> b6 = new Basket<?>();
Ans. a, b, c, f
But according to me, the answer should be a,c and f.
I did not get why ans b is also correct.
Can anyone through a light on it.
Thanks <br /> <br />Regards,<br />------------------------------<br />Bijendra S. Rajput<br />SCJP 1.5<br />------------------------------
Joined: Feb 07, 2005
Feb 21, 2007 06:35:00
Remember that generics are implemented through type erasure, so Basket<Fruit> is a Basket. When you use a raw type though you lose the type checking at compile-time.
I agree. Here's the link:
subject: Doubt in a question of Generics
javabeat mock test
generics question from javabeat
method types in Generics
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014