About Java generics, here I do not want to summarize what it is, this Baidu a lot of explanations, all kinds of Java books also have a clear definition, as long as a little look can be quickly clear. From the generic English name generic type can also be seen, generic ordinary, general, general, is a generalization of the word, then generics from the name is also good to understand, it is a common type, is a generalization of various types in Java.
? is a wildcard character in a Java generic that represents a class in Java, then < Extends t> represents a subclass of type T, < Super T> represents a parent class of type T.
Here we first define a set of classes with inheritance relationships:
// Child-to- parent class Little Red Apple -- Red apple -- apple -- Fruit -- delicious -- eat
These classes are subclasses of the class on the left that are connected to the right side of the class.
So <? Extends Apple > represents a Class in the left-hand blue and green-word classes, while < Super Apple > represents a Class in a class of green and red characters .
The attention here is < Extends t> or <? Super T> represents a particular class within a range, not all classes within a range.
// so as long as within the scope, we can do the following random assignment extends apple > new arraylist< Apple >(); Listextends apple > new arraylist< Red Apple >(); Listextends apple > new arraylist< Little Red apple > ();
But for list< Extends Apple > list, which represents a class within a range, but is not sure which class, so if we add elements to this list:
extends apple > new arraylist< Apple >(); List.add (apple); // compilation error list.add (red apple); // compilation error List.add (Little Red apple); // Compile Error
Because the compiler does not know which class the list is (only when it is run to determine which class to refer to), if the list is a red apple, then List.add (Apple) assigns a parent class to the subclass, which is wrong. Obviously if you add a class to this list, are not guaranteed to be correct. May say that little red Apple has no sub-class, add little Red apple can not be wrong, but this is just my definition of an inheritance diagram is so, we could continue to define a small red apple to inherit the Little Red Apple, this inheritance is no lower limit. This anti-launch a conclusion < Extends t> is a type with an upper limit of T . So we immediately found < Super T> is actually a type with a lower bound T .
Because for < Extends t> has an upper limit of T, so we if List.get (0) must return is T or the subclass of T, this is determined, to obtain:
extends apple > new arraylist< apple >= list1.get (0); // This is a certain set up, the compilation will not be a problem
list<? Extends Apple > list2 = new arraylist< Red apple >(); Apple a = List2.get (0);
list<? Extends Apple > list3 = new arraylist< Little Red apple >
Then we look at < Super T> because it has a lower bound, we can immediately conclude that it is no problem to add an object of type T to it. Because of < Super T> is a parent class of T, and assigning a subclass T to a parent class has no problem:
Super Apple > new arraylist< Apple >(); List.add (apple); // without any problems Super Apple > new arraylist< fruit >(); List.add (apple); // without any problems
On the < of Java generics; Extends t> and <? The difference between Super t>