Content deleted Content added
Fixed formatting issues. |
Add code formatting. |
||
Line 12:
In the body of a generic unit, the (formal) type parameter is handled like its [[bounded quantification|upper bound]] (expressed with <code>'''extends'''</code>; <code>Object</code> if not constrained).{{sfn|Bloch|2018|loc=Chapter §5 Item 31: Use bounded wildcards to increase API flexibility|pp=139-145}} If the return type of a method is the type parameter, the result (e.g. of type <code>?</code>) can be referenced by a variable of the type of the upper bound (or <code>Object</code>). In the other direction, the wildcard fits no other type, not even <code>Object</code>: If <code>?</code> has been applied as the formal type parameter of a method, no actual parameters can be passed to it. However, objects of the unknown type can be read from the generic object and assigned to a variable of a supertype of the upperbound.
Sample code for the <code>Generic<T '''extends''' UpperBound></code> class:
<syntaxhighlight lang="java">
class Generic <T extends UpperBound> {
Line 22 ⟶ 23:
}
}
</syntaxhighlight>
Sample code that uses the <code>Generic<T '''extends''' UpperBound></code> class:
<syntaxhighlight lang="java">
...
final Generic<UpperBound> concreteTypeReference = new Generic<UpperBound>();
final Generic<?> wildcardReference = concreteTypeReference;
final UpperBound ub = wildcardReference.read(); // Object would also be OK
wildcardReference.write(new Object()); // type error
wildcardReference.write(new UpperBound()); // type error
concreteTypeReference.write(new UpperBound()); // OK
...
</syntaxhighlight>
Line 65 ⟶ 71:
A <code>List<? '''extends''' MyClass></code> is a list of objects of some subclass of <code>MyClass</code>, i.e. any object in the list is guaranteed to be of type <code>MyClass</code>, so one can iterate over it using a variable of type <code>MyClass</code><ref>[[Inheritance (object-oriented programming)]]</ref>
<syntaxhighlight lang="java">
public void doSomething(final List<? extends MyClass> list) {
for (final MyClass object : list) { // OK
// do something
}
Line 73 ⟶ 79:
However, it is not guaranteed that one can add any object of type <code>MyClass</code> to that list:
<syntaxhighlight lang="java">
public void doSomething(final List<? extends MyClass> list) {
final MyClass m = new MyClass();
list.add(m); // Compile error
}
Line 82 ⟶ 88:
A <code>List<? '''super''' MyClass></code> is a list of objects of some superclass of <code>MyClass</code>, i.e. the list is guaranteed to be able to contain any object of type <code>MyClass</code>, so one can add any object of type <code>MyClass</code>:
<syntaxhighlight lang="java">
public void doSomething(final List<? super MyClass> list) {
final MyClass m = new MyClass();
list.add(m); // OK
}
Line 89 ⟶ 95:
However, it is not guaranteed that one can iterate over that list using a variable of type <code>MyClass</code>:
<syntaxhighlight lang="java">
public void doSomething(final List<? super MyClass> list) {
for (final MyClass object : list) { // Compile error
// do something
}
|