aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generics - why does this work? 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 - why does this work?" Watch "generics - why does this work?" New topic
Author

generics - why does this work?

Lighted Jones
Greenhorn

Joined: Jun 29, 2007
Posts: 5
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

Joined: Feb 07, 2005
Posts: 2367
But a D object is an A object.
Lighted Jones
Greenhorn

Joined: Jun 29, 2007
Posts: 5
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

Joined: Jun 04, 2007
Posts: 377
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jun 29, 2007
Posts: 5
Right......

Yes I see what you mean..

Thanks guys.
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111

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



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




Thanks
samura
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111


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

Joined: Jun 04, 2007
Posts: 377
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

Joined: Aug 16, 2005
Posts: 14433
    
  23

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generics - why does this work?