Jerry Brady
Jerry Brady

Reputation: 3080

Issue with generic types of ArrayList and interface inheritance

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?

Upvotes: 7

Views: 3293

Answers (2)

Bohemian
Bohemian

Reputation: 424983

To "fix" the code, you need to use a generic bound:

public void interfaceIsTheArgument(List<? extends Weapon> w) { ... }
...
interfaceIsTheArgument(new ArrayList<? extends Weapon> ());
interfaceIsTheArgument(new ArrayList<Knife>());

The key reason is List<Gun> is not a subclass of List<Weapon>. The reason for this fact can be illustrated by this code:

List<Gun> guns = new ArrayList<Gun>();
// If List<Weapon> was a super type of List<Gun>, this next line would be allowed
List<Weapon> weapons = guns; // Won't compile, but let's assume it did
weapons.add(new Knife());  // Compiles, because Knife is a Weapon
Gun gun = guns.get(0); // Oops! guns.get(0) is a Knife, not a Gun!

By using the bound <? extends Weapon>, we are saying we'll accept any generic type that is a subclass of Weapon. Using bounds can be very powerful. This kind of bound is an upper bound - we are specifying the top-level class as being Weapon.

There's also a lower bound, that uses this syntax:

List<? super Weapon> // accept any type that is a Weapon or higher in the class hierarchy

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) use extends, and on the consumer side of the code (where the objects are used) us super. Once you try it a few times, you'll understand through experience why it works well.

This SO question/answer covers it well.

Upvotes: 13

JB Nizet
JB Nizet

Reputation: 691655

This is one of the most asked questions about generics. It a List<Gun> was a List<Weapon>, you could do

List<Gun> gunList = new ArrayList<Gun>();
List<Weapon> weaponList = gunList;
weaponList.add(new Knife());
gunList.get(0).fire(); // ClassCastException

This would thus break the type-safety promised by generics.

Upvotes: 4

Related Questions