I’m having trouble understanding why the following doesn’t work and I’m sure the answer is related to something basic I am not understanding and hope someone can help.
I understand about using interfaces in an ArrayList such that if I have:
public interface Weapon { ... }
public class Gun implements Weapon { ...}
public class Knife implements Weapon { ... }
you can then insert anything that implements Weapon into the an array of weapons:
ArrayList<Weapon> weapons = new ArrayList<Weapon>();
weapons.add(new Gun());
weapons.add(new Knife();
I get that, but what is confusing me is the understanding of why ArrayList<Gun> isn’t compatible with ArrayList<Weapon> in other ways. To illustrate, the following is legal:
public void interfaceIsTheArgument(Weapon w) { ... }
...
interfaceIsTheArgument(new Gun());
interfaceIsTheArgument(new Knife());
but the following is not:
public void interfaceIsTheArgument(ArrayList<Weapon> w) { ... }
...
interfaceIsTheArgument(new ArrayList<Gun>());
interfaceIsTheArgument(new ArrayList<Knife>());
because the last function call reports that the method isn’t applicable for its arguments.
My question is why if the method knows it tasks an ArrayList with an interface as the generic type, why isn’t it okay to pass in an array list of knives in that last statement?
To “fix” the code, you need to use a generic bound:
The key reason is
List<Gun>is not a subclass ofList<Weapon>. The reason for this fact can be illustrated by this code:By using the bound
<? extends Weapon>, we are saying we’ll accept any generic type that is a subclass ofWeapon. Using bounds can be very powerful. This kind of bound is an upper bound – we are specifying the top-level class as beingWeapon.There’s also a lower bound, that uses this syntax:
So, when to use each one? Remember this word
PECS: “Producer extends, consumer super”. This means on the producer side of the code (where the objects are created) useextends, and on the consumer side of the code (where the objects are used) ussuper. Once you try it a few times, you’ll understand through experience why it works well.This SO question/answer covers it well.