• 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-compiler error

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

Guitar and Violen extend Instrument even then its giving compiler error i dont understand this please help.....
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


List<? extends Instrument> list=new ArrayList<Instrument>();



with extends keyword we cannot add anything to list.

That is why compiler error is coming up.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When List<? extends Instrument> list is used, list can be assigned a collection that is a subtype of List and typed for <Instrument> or anything that extends Instrument.
And you cannot ADD anything into the collection.
If you try to add, then it will result in compiler error.

If you want to add elements to the collection, then you can use
List<? super Instrument> list.
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok but then whats the use of <? extends> when we cannot add anything to it??
 
Brij Garg
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


List<Guitar> guitar = new ArrayList<Guitar>
List<Voilen> voilen = new ArrayList<Voilen>


--------------------------------------------------


public void test(list<? extends Instrument> list)
{
Instrument i= list.get(0);
i.play();
}


-------------------------------------------------
Now you can call test(guitar) and also test(voilen).

This is jut one example.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<? extends XXX > is usually used as a parameter in a method signature.
For instance, the following method
public void printList(List<? extends Shape> shapes) {
...
}
can be passed a list of any objects which are extended from Shape.
If you have two classes which extends Shape
public class Square extends Shape{
..
// create List of Square objects
}
and public class Circle extends Shape{
..
// create List of Circle objects
}

You can pass a list of both Circle or Square objects to the printList() method as declared above.
Hope this makes sense

:roll:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic