aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generics doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generics doubt" Watch "generics doubt" New topic
Author

generics doubt

teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51
In the below code when i try to add a1.add(new A() /new B() /new C()) its not compiling the method(a1.add()) is taking only null.
why so? what actually this ArrayList<? extends B> a1=new ArrayList() for ? what objects can we add to a1?



SCJP 5
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Please UseCodeTags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi,

Look at your method signature



When you say



It means that, you can pass any arraylist which is a subtype of B, but it will be a read-only collection. You cannot add anything into the list.
This is to make sure that you don't add anything wrong inside the list, because in generics due to type-erasure, no run-time information exists
about the type. That's why compiler restricts you to add anything into a read-only collection.

Hope this helps,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51
Not satisfied with your answer better you explain with a clean example for extends in generic no urgent take your own time.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Tell me that,

What will happen if we stored something like a class D object which extends from B and is a sibling to C and the
arrayList which is being passed is of type C. What will happen then? Wrong type will be added to the collection
ain't it?

With array type information exists at run-time and if even we add a wrong type inside that array, we have got something
like ArrayStoreException. So you cannot add any wrong-type into the arrays. What about generics. No such exception exists because at
run-time JVM has no information about the type of the list. At run-time it behaves as a normal Pre-Java 5 list. Okay if it would have allowed you
to store that wrong type into the collection, what will happen when you will try to get something out of the list thinking that it is of type but it
turned out to be D. At that time you are dead.

Generics only provides compile time safety, not run-time.

By making it sure that you don't add anything wrong into the list, it is saving you from future disaster.

Still confused or want to give you a more detailed example?

Think about my reasons.

Warm Regards,
teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51
k ..what i exactly mean is this "ArrayList<? extends B> a1 " can hold only the objects of B and its subclasses am i right?
If not what sort of objects it can hold exactly?
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hi,

I doubt that we can add anything to the list. The reference has to be used to call methods so that w can get
polymorphic behavior. If you want to add something to the list, you got to change it to somewhat

If we are mantaining Dog,Cat, Animal heirarchy



Hope this helps,
teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51
Prithvi Sehgal wrote:Hi,

I doubt that we can add anything to the list. The reference has to be used to call methods so that w can get
polymorphic behavior. If you want to add something to the list, you got to change it to somewhat

If we are mantaining Dog,Cat, Animal heirarchy


k fine List<? super Dog> animalList for adding Dog or sub-type of Dog .What about List<? extends Dog> animalList you mean its impossible to add objects for animallist .If so why?If not how?I hope this discussion will be closed with your coming reply.
Hope this helps
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Prithvi Sehgal wrote:Tell me that,

What will happen if we stored something like a class D object which extends from B and is a sibling to C and the
arrayList which is being passed is of type C. What will happen then? Wrong type will be added to the collection
ain't it?

With array type information exists at run-time and if even we add a wrong type inside that array, we have got something
like ArrayStoreException. So you cannot add any wrong-type into the arrays. What about generics. No such exception exists because at
run-time JVM has no information about the type of the list. At run-time it behaves as a normal Pre-Java 5 list. Okay if it would have allowed you
to store that wrong type into the collection, what will happen when you will try to get something out of the list thinking that it is of type but it
turned out to be D. At that time you are dead.

Generics only provides compile time safety, not run-time.

By making it sure that you don't add anything wrong into the list, it is saving you from future disaster.

Still confused or want to give you a more detailed example?

Think about my reasons.

Warm Regards,


Ok now that being said, why don't you go through this post again and think over and tell me. If you understand this post, you will get your
answer.


? extends is just for polymorphic behavior. You cannot add anything inside. Do refer back to the above post again. Else i will give you one detailed
example.

Best Regards,
teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51


Thank you prithvi i understood explanation. If you dont mind could you provide a detailed example for
List<? extends Dog> animalList i hope you will.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello,

Please have a look at GenericsConfusion

I have tried to explain the concept, if still don't understand. Refer back.

Hope this helps,
teja dharma
Ranch Hand

Joined: Feb 07, 2009
Posts: 51
Prithvi Sehgal wrote:Hello,

Please have a look at GenericsConfusion

I have tried to explain the concept, if still don't understand. Refer back.

Hope this helps,

Excellent work Prithvi you cleared my doubt with a detailed example. Keep going. .
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello,

My pleasure Teja. I am happy that i wrote something which you understood.

Cheers,
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: generics doubt