As you already know, it's possible to assign an object of one type to an object of another type provided that the types are compatible. For example, you can assign anInteger
to anObject
, sinceObject
is one ofInteger
's supertypes:Object someObject = new Object(); Integer someInteger = new Integer(10); someObject = someInteger; // OKIn object-oriented terminology, this is called an "is a" relationship. Since anInteger
is a kind ofObject
, the assignment is allowed. ButInteger
is also a kind ofNumber
, so the following code is valid as well:public void someMethod(Number n){ // method body omitted } someMethod(new Integer(10)); // OK someMethod(new Double(10.1)); // OKThe same is also true with generics. You can perform a generic type invocation, passing
Number
as its type argument, and any subsequent invocation ofadd
will be allowed if the argument is compatible withNumber
:Box<Number> box = new Box<Number>(); box.add(new Integer(10)); // OK box.add(new Double(10.1)); // OKNow consider the following method:
public void boxTest(Box<Number> n){ // method body omitted }What type of argument does it accept? By looking at its signature, we can see that it accepts a single argument whose type isBox<Number>
. But what exactly does that mean? Are you allowed to pass inBox<Integer>
orBox<Double>
, as you might expect? Surprisingly, the answer is "no", becauseBox<Integer>
andBox<Double>
are not subtypes ofBox<Number>
.Understanding why becomes much easier if you think of tangible objects — things you can actually picture — such as a cage:
// A cage is a collection of things, with bars to keep them in. interface Cage<E> extends Collection<E>;
Note: TheCollection
interface is the root interface of the collection hierarchy; it represents a group of objects. Since a cage would be used for holding a collection of objects (the animals), it makes sense to include it in this example.
A lion is a kind of animal, so
Lion
would be a subtype ofAnimal
:interface Lion extends Animal {} Lion king = ...;Where we need some animal, we're free to provide a lion:
Animal a = king;A lion can of course be put into a lion cage:
Cage<Lion> lionCage = ...; lionCage.add(king);and a butterfly into a butterfly cage:
interface Butterfly extends Animal {} Butterfly monarch = ...; Cage<Butterfly> butterflyCage = ...; butterflyCage.add(monarch);But what about an "animal cage"? English is ambiguous, so to be precise let's assume we're talking about an "all-animal cage":
Cage<Animal> animalCage = ...;This is a cage designed to hold all kinds of animals, mixed together. It must have bars strong enough to hold in the lions, and spaced closely enough to hold in the butterflies. Such a cage might not even be feasible to build, but if it is, then:animalCage.add(king); animalCage.add(monarch);Since a lion is a kind of animal (
Lion
is a subtype ofAnimal
), the question then becomes, "Is a lion cage a kind of animal cage? IsCage<Lion>
a subtype ofCage<Animal>
?". By the above definition of animal cage, the answer must be "no". This is surprising! But it makes perfect sense when you think about it: A lion cage cannot be assumed to keep in butterflies, and a butterfly cage cannot be assumed to hold in lions. Therefore, neither cage can be considered an "all-animal" cage:animalCage = lionCage; // compile-time error animalCage = butterflyCage; // compile-time errorWithout generics, the animals could be placed into the wrong kinds of cages, where it would be possible for them to escape.