• 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

Confusing logic in Generics

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

class A{}
class B extends A{}

List<? super A> list = new ArrayList<A>(); //line 1
list.add(new A()); //line 2
list.add(new B());

The logic for this says that:
At line 1, the generic type of right hand side of equals should satisfy the generic type on left hand side.
in this case it is true.
And if its replaced with List<? super B> list = new ArrayList<A>(); , then its still appropriate.
AND,
if its replaced with List<? super A> list = new ArrayList<B>();, then its not appropriate as B is not super of A.

BUT,
line 2 onwards, while adding elements into the list, they must be the type or sub-type of the generic type -
means we can add A, subtypes of A.

Now, the confusing part.
List<? extends A> list = new ArrayList<A>(); //line 1
list.add(new A()); //line 2
list.add(new B());

when 'extends' is used, then too the same logic is applicable?
i.e is it that only subtypes of A (in this case) are allowed to be added?


 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
using extends, we cannot add anything
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Prasad for your reply.

but,
List<? extends A> list = new ArrayList ();
list.add(new A()); too cannot be done??

<Edit> --> Answer: No it cannot be done.
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rikesh
I think this can be solved by just checking whether you can do it or not
just by writing a simple 10-15 line program
what can you do with extends and super

take it as friendly advice as I would like you to learn more and more by doing everything on your own
if you still don't get about those concepts
then feel free to ask

but I really think that you should try it out first
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok Prasad.
will share with you my findings after trying out some sample codes.
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

I have been trying to code different flavours of generics :



I am unable to understand why am I getting a compiler error though I have kept the signature of the generic declaration same..

Compiler error


Any pointers to explaining all the rules of generics would be very helpful.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rikesh,

Generics can get a little sticky and confusing. All I can suggest apart from keep writing code to test your understanding, is to read read read. Particularly helpful for me was the concept of Upper and Lower bounds. That is the and generic references.

Just a little tip for ya, good luck
 
Rikesh Desai
Ranch Hand
Posts: 83
thanks Stephen,

have already cleared OCPJP and i scored 100% in Collections and Generics!

but nice advice though.
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic