Wildcard (Java): Difference between revisions

Content deleted Content added
Bounded Wildcards: elaborate/improve exposition
Line 32:
== Bounded Wildcards ==
 
A bounded wildcard is one with either an upper or a lower [[Inheritance (object-oriented programming)|inheritance]] constraint. The Notbounds onlycan thebe formalboth typeclass parametersand ininterface types. Upper bounds are expressed using the generic'''extends''' unit,keyword butand alsolower bounds using the '''super''' kkeyword. An upper bound on a wildcard canmust be (further)a constrainedsubtype ifof onethe doesn'tupper wantbound toon bethe compatibletype withparameter all instantiations:it is assigned.
 
<code>Generic<? '''extends''' SubtypeOfUpperBound> referenceConstrainedFromAbove;</code>
 
This reference can hold any instantiation of <code>Generic</code> with an actual type parameter of <code>SubtypeOfUpperBound</code>'s subtype. A wildcard that does not have aan constraintupper bound is effectively the same as one that has the constraint <code>extends Object</code>, since all types implicitly extend Object. A constraintwildcard with a lower bound
 
<code>Generic<? '''super''' SubtypeOfUpperBound> referenceConstrainedFromBelow;</code>