Hi people. I am wrecking my brain on this issue about generics and ? extends type. My reference for the question is the Khalid&Rassmussen book for the SCJP6, questions 14.15. We've got the following classes:
Why in the world is it not possible to *put* any values into *g* ?
There was one more thread where we had a discussion about <?>, <? super > and < ? extends>. I was looking for that thread but the guy who asked the question misbehaved and apparently, the thread was deleted. Bummer!
... Since you've already specified you will only add types of car and subclasses, it should be allowed to add these types. But you're not.
Note that you have just specified the UPPER BOUND on type and not the actual type that will be passed. The actual type will only be known only at run time. GenericType<Car> is not same as GenericType<Sedan>. If at runtime you use GenericType<Sedan> you cannot add a Car object to it. The cast will fail.
I would think we should be able to add object, vehicle and car.
How can you add object,vehicle and car if the object made at runtime is a specific vehicle called subaru or honda? If it is <? extends Type> means the particular collection can have the type or its subtypes but you cannot add anything as the compiler isn't sure what the type safe object at runtime.
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
You can't have everything. Where would you put it?