Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question related to use of super in generic method argument

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you help me understand why i have to explicitly cast the Dog when using super in generic method argument

This does not work

But this works


Appreciate your help.

Regards,
Sachin Deokar
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever we are using <? super something> with collection, we are not sure about the values that will be added to the collection. Here we can enter anything that is super to something. It could be superSomething or Object. So the return type in this case will b Object. That's why you can not use for each loop using Dog object in you code.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<? super className>

Object is the superClass of all the classes, That is why it expects an Object return Type(as at max we can return an Object type).
 
Sachin Deokar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys .. Here's another question related to generics

Why is the use of extends allowed in declaration when we cannot add anything to the resulting Collection Object?

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't add elements to that collection but can take out elements from it right. So I can design a method which takes a list of Serializable objects and serialize them.
This is why this type of declaration is allowed...
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Deokar wrote:Thanks guys .. Here's another question related to generics

Why is the use of extends allowed in declaration when we cannot add anything to the resulting Collection Object?



Wildcards will suit for methods, Otherwise we can't add anything to those collections!
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because then the collection will not be generic . /we will not be sure about the type of element we can add to that collection as as many classes can extend that particular class specified with wild card.
But in earlier case you were sure that the class can be at most of specific type.
 
Sachin Deokar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand the use of wildcards for method arguments. My question was regarding the specific line of code.

If we create an ArrayList using the wildcard along with extends, we still wont be able to add anything to that ArrayList. So why is it allowed to be used in variable declaration even though we end up with an Collection to which nothing can be added?

Abimaran Kugathasan wrote:
Wildcards will suit for methods, Otherwise we can't add anything to those collections!

 
Sachin Deokar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was trying to understand the difference between

and

Is it possible to add a new Animal object to animal_2? If not then why?

Harpreet Singh janda wrote:Because then the collection will not be generic . /we will not be sure about the type of element we can add to that collection as as many classes can extend that particular class specified with wild card.
But in earlier case you were sure that the class can be at most of specific type.

 
Sachin Deokar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I didn't read your reply before posting my earlier messages. I feel so dumb to have missed such a simple point.

As always thanks for your valuable input.

Sachin

Ankit Garg wrote:You can't add elements to that collection but can take out elements from it right. So I can design a method which takes a list of Serializable objects and serialize them.
This is why this type of declaration is allowed...

 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, You van not add anything to that list. List will be read only.

Why- I think the answer is type safety. Generics are introduced for the purpose of type safety. So that you can guard you collection from accepting not-allowed elements. As we are not sure about the elements that would come to our list so java has marked it as read only.
 
Sachin Deokar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Harpreet. Finally I am getting hang of generics. Always used to get confused with the use of wild cards in method arguments.

Sachin

Harpreet Singh janda wrote:No, You van not add anything to that list. List will be read only.

Why- I think the answer is type safety. Generics are introduced for the purpose of type safety. So that you can guard you collection from accepting not-allowed elements. As we are not sure about the elements that would come to our list so java has marked it as read only.

 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic