• 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

Difference between two notations of java Generics ?

 
Greenhorn
Posts: 19
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks !

Actually i am using java generics in my project and get confused in one thing, let's say i have two lists.

Now, in both cases "list1" and "list2" both are able to take child class objects, So question is

What is the difference between two notations "<? extends BaseClass>" and "<BaseClass>" ? which one is better to use ?

Regards,
SD Khan
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first one is unspecified because it can be anything that extends BaseClass. Say, classes A and B extend BaseClass. You can't add As or Bs to the first list because then you might add both As and Bs to the same list which is not allowed by the intention of that generic. To have a list which takes both As and Bs you would need to use the second one List<BaseClass> .

You use the first one when coding to the interface of some API that will process a list of anything that extends BaseClass (but containing only one type).
 
Safi Khan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi @Armitage , thanks for your answer.

I got you concept but there is till a little ambiguity, let's check the sample code


Now, list1 is excepting all the objects of that are extended from base class, but list2 gives the compile time error on this line



As according to the concept, list2 should accept that object but why it's behavior is like that ?
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I already explained above that you can't add to a List<? extends BaseClass> because List<? extends BaseClass> means List of objects of only one type where that type extends from BaseClass. Importantly, with List<? extends BaseClass> the type of the list is unspecified (unknown). The only known thing is that the type extends from BaseClass and that information is not enough to allow anyone to add objects into it. If you are allowed to add childClass1 objects then you would be allowed to add childClass2 objects as well which would result in a list with different types.
List<BaseClass> means list of objects of type BaseClass and all subclasses of BaseClass so adding is allowed because the definition of the List is specific.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't use heavy red text; that violates this principle. I also had to break your long line because it makes the post difficult to read.
 
Safi Khan
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Armitage, Yup, now i got that, thanks !

@Campbell Ritchie, sorry !, i will take care of this next time.

Thanks,
SD Khan
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apology accepted
 
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

E Armitage wrote:I already explained above that you can't add to a List<? extends BaseClass> because List<? extends BaseClass> means List of objects of only one type where that type extends from BaseClass. Importantly, with List<? extends BaseClass> the type of the list is unspecified (unknown). The only known thing is that the type extends from BaseClass and that information is not enough to allow anyone to add objects into it. If you are allowed to add childClass1 objects then you would be allowed to add childClass2 objects as well which would result in a list with different types.
List<BaseClass> means list of objects of type BaseClass and all subclasses of BaseClass so adding is allowed because the definition of the List is specific.



I don't understand this.
Then what is the purpose of <? extends Object>

Note. I NEVER used ArrayList<? extends Object> before.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<? extends Object> is practically the same as <?>.
 
Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:<? extends Object> is practically the same as <?>.


Thanks Rob!

But I mean this,

Consider this


if we can't add an integer to arraylist what's the purpose of it?

Thanks
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Supun Lakshan Dissanayake wrote:

Rob Spoor wrote:<? extends Object> is practically the same as <?>.


Thanks Rob!

But I mean this,

Consider this


if we can't add an integer to arraylist what's the purpose of it?

Thanks


in your example, arrayList is an array list of some type X, where X could be sub-type of Number
i.e. it could be array list of Number, Integer, Double, etc.

In the case of Double, it could not hold Integer. So, you got compile time error.

According to PECS (Producer Extends, Consumer Super), if your collection is going to consume some value, it should use super instead of extends.
which may be
 
It's a pleasure to see superheros taking such an interest in science. And 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