• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generics - why does this work?

 
Lighted Jones
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But a D object is an A object.
 
Lighted Jones
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes but generic types must be adhered to strictly, unlike for arrays.

for example in an

A [] arrayA = new [] A ;

In the case above we can add objects of type A, B, C , D to arrayA.

But for
List<A> la = new ArrayList<A>();

we cannot add B,C, or D objects to la because generic types are strict.

Therefore in the example I posted shouldn't List duane reject any non A additions?

or am I wrong?
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lighted Jones:
But for
List<A> la = new ArrayList<A>();

we cannot add B,C, or D objects to la because generic types are strict.

You can. What you can't do is something like this:
List<A> la = new ArrayList<B>();
Which will work with arrays without problems.
A[] a = new B[10];
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also consider the case of a List<Object>.

What kind of elements can you add to the list? Anything that is an Object.
 
Lighted Jones
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right......

Yes I see what you mean..

Thanks guys.
 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

md=[cereal.D@126b249, cereal.D@182f0db]



may i know what is the above one in the output.




Thanks
samura
 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


originally posted by Lighted Jones:

List<? super D> duane = new ArrayList < A >();


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
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Lighted Jones,

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.

Have fun on JavaRanch,
Jesper Young - Bartender
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic