• 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

Reference assignment involving Generics and hierarchy- Niko's Blog

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source- Niko's SCJP Mock, Generics

There are lot of related questions on assignment and hierarchy, few of them were-




There are some of them which uses ? super. How to go about solving such questions?

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well for a detailed explanation and concept building, i would suggest you to read the chapter of Collections and Generics from K & B thoroughly.

As for the first code, <? extends Number> means any class that is a subclass of class Number can come here. Since Integer is a subclass of Number , and list2 is a List of type Integer , list1 = list2 will compile without any errors.

for the second code , list one is defined to be of type Integer ( a fixed definite type) so any list of only the type Integer can be assigned to it. but on the right hand side we find that , we get list of type <? extends Number> so compilation fails.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saima kanwal wrote:well for a detailed explanation and concept building, i would suggest you to read the chapter of Collections and Generics from K & B thoroughly.



Yeah i have gone through the chapters. But its a bit confusing.

saima kanwal wrote:As for the first code, <? extends Number> means any class that is a subclass of class Number can come here. Since Integer is a subclass of Number , and list2 is a List of type Integer , list1 = list2 will compile without any errors.

for the second code , list one is defined to be of type Integer ( a fixed definite type) so any list of only the type Integer can be assigned to it. but on the right hand side we find that , we get list of type <? extends Number> so compilation fails.



Thanks for the explanation. Just cleared the confusion
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic