This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
Why does the code below generate the following output? md=[cereal.D@126b249, cereal.D@182f0db] class type of duane is class cereal.D wierdGen=cereal.D@126b249
The List called duane is typed to A but since the reference variable is ? super D its allowing me to successfully add D type objects to the List thats typed to A?
Shouldn't I get a runtime error when I add the D object into the List?
Output is md=[cereal.D@126b249, cereal.D@182f0db] class type of duane is class cereal.D wierdGen=cereal.D@126b249 [ July 08, 2007: Message edited by: Lighted Jones ]
we can add anything which is super of D. it is declared in the LHS of duane. what about the actual object side(RHS) ArrayList<A>() means what? why we need to say this? when we can add all super of D and even retrieve it without run time exception.
Thanks samura
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Originally posted by samura babu: we can add anything which is super of D.
That is not correct. The super keyword refers to the lists that can be used. Have a look at the following sample.As you can see, it would be easy to add an element of the wrong type. The only type that is valid for all possible lists is D.
I am one of the moderators of the JavaRanch forums. Here on JavaRanch, we have a naming policy. We want the users of JavaRanch to use a real name as their display name.
Unfortunately, your name "Lighted Jones" does not comply to the naming policy. Your name should consist of a first name, space, and second name and it should not be an obviously fake name.
Please read the naming policy carefully and change your display name. You can change your name by editing your profile.
Please note that we are taking the naming policy seriously. If you do not change your display name, your account on JavaRanch might be deleted.