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?
public void interfaceIsTheArgument(ArrayList<? extends Weapon>). That will work.