5

Where can we ArrayList<? extends My_Class> because it won't allow you to add any new element if you declare it of this way:

ArrayList<? extends My_Class> obj=new ArrayList<? extends My_Class>();
list.add(new Derived_My_Class()); //this is compilation error
Alonso Dominguez
  • 7,750
  • 1
  • 27
  • 37
  • possible duplicate of [How can elements be added to a wildcard generic collection?](http://stackoverflow.com/questions/176446/how-can-elements-be-added-to-a-wildcard-generic-collection) – John Conde Jul 27 '12 at 02:23

3 Answers3

5

The widely-used acronym for describing these keywords is: PECS: Producer extends - consumer super.

So, if you use "extends" your collection produces elements of the given type. So you can't add to it, you can only take from it.

An example usage would be to provide a client of your API with a list:

public List<? extends CharSequence> getFoo() {
    List<String> list = new ArrayList<String>();
    list.add("foo");
    return list;
}

Related: Java generics: Collections.max() signature and Comparator

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • What is the advantage we are trying to get out of it i don't understand because the caller of this method also should define a variable of type List extends CharSequence> not even List and later also u can't modify the received variable that's why i don't understand why do we need it – user1554627 Jul 26 '12 at 13:22
2

You are correct, your ArrayList cannot be used the way it is derived: there is nothing that you can put into it.

What you want for storing My_Class and its subclasses is simply ArrayList<My_Class>; it will take objects of My_Class and all its DerivedClass correctly.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Well...There is no point one will declare ArrayList like this, rather your intentions will be fullfilled by writing

ArrayList< Supertype> obj=new ArrayList< Supertype>();

As per my epxerience, I have seen this notion in method arguments, where you expect your caller to provide a collection of subtypes of particular Supertype (or return from a method likewise as someone said above). like as follows

public getAnimals(List< ? extends Animal> obj){

obj.add(something);    //not allowed

}

there are fair chances that you can add donkeys, monkeys and birds etc in your List of certain type say (Monkey). and get classCastException while getting from it.

That's why It not allowed in this case. read Effective Java by Josh Bloch. he has explained it well with producer consumer analogy(PECS)

Ahmad
  • 2,110
  • 5
  • 26
  • 36
  • Ahm... What? Yes, that's exactly what I would want in certain cases. Does not adress the question – Niklas B. Jul 26 '12 at 12:34
  • supposing that donkeys, monkeys and birds all of them derive from `Animal` then... what's the problem for storing them in a `ArrayList`?? and... where is the potential disaster?? – Alonso Dominguez Jul 26 '12 at 12:35
  • I don't expect myself to be such a dumb. i am sorry..refer updated answer – Ahmad Jul 26 '12 at 12:47