• 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

Generics doubt

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList<? extends Number> l = new ArrayList<Integer>();
l.add(new Integer(12));
// compile time error

we can not add a element to wild card generic type reference.

ArrayList<? super Number> list = new ArrayList<Number>();
list.add(new Float(12.3));

adding Float object which is not super class of Number.

Super Rule is add the super class of Number object or lower bound object

please explain how it is compiling with out error
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sentil kumar:
ArrayList<? extends Number> l = new ArrayList<Integer>();
l.add(new Integer(12));
// compile time error

we can not add a element to wild card generic type reference.

ArrayList<? super Number> list = new ArrayList<Number>();
list.add(new Float(12.3));

adding Float object which is not super class of Number.

Super Rule is add the super class of Number object or lower bound object

please explain how it is compiling with out error



The list can hold a Number or Object. Since a Float is a Number, you can add it.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic