• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

generics doubt

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when you post source code.
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
teja dharma
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

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

Cheers,
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic