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?
As of Java SE 5.0, ArrayList is a generic class with a type parameter. To specify the type of the element objects that the array list holds, you append a class name enclosed in angle brackets, such as ArrayList<Employee>.
Multiple Inheritance is not supported by class because of ambiguity. In case of interface, there is no ambiguity because implementation to the method (s) is provided by the implementing class up to Java 7. From Java 8, interfaces also have implementations of methods.
Comparable interface is a great example of Generics in interfaces and it’s written as: package java.lang; import java.util.*; public interface Comparable<T> { public int compareTo (T o); } In similar way, we can create generic interfaces in java. We can also have multiple type parameters as in Map interface.
Generic Classes and Subtyping. You can subtype a generic class or interface by extending or implementing it. The relationship between the type parameters of one class or interface and the type parameters of another are determined by the extends and implements clauses. Using the Collections classes as an example,...
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With