{"id":415,"date":"2007-10-21T00:00:00","date_gmt":"2007-10-21T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=415"},"modified":"2007-10-21T00:00:00","modified_gmt":"2007-10-21T00:00:00","slug":"Wildcards","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=415","title":{"rendered":"Wildcards"},"content":{"rendered":"<p><DIV id=PageTitle>Wildcards<\/DIV><br \/>\n<BLOCKQUOTE>Earlier we mentioned that English is ambiguous. The phrase &#8220;animal cage&#8221; can reasonably mean &#8220;all-animal cage&#8221;, but it also suggests an entirely different concept: a cage designed not for <I>any<\/I> kind of animal, but rather for <I>some<\/I> kind of animal whose type is unknown. In generics, an unknown type is represented by the <I>wildcard<\/I> character &#8220;<CODE>?<\/CODE>&#8220;.<br \/>\n<P>To specify a cage capable of holding <I>some<\/I> kind of animal: <\/P><PRE>\tCage&lt;? extends Animal&gt; someCage = &#8230;;<br \/>\n<\/PRE>Read &#8220;<CODE>? extends Animal<\/CODE>&#8221; as &#8220;an unknown type that is a subtype of <CODE>Animal<\/CODE>, possibly <CODE>Animal<\/CODE> itself&#8221;, which boils down to &#8220;some kind of animal&#8221;. This is an example of a <I>bounded wildcard<\/I>, where <CODE>Animal<\/CODE> forms the <I>upper bound<\/I> of the expected type. If you&#8217;re asked for a cage that simply holds <I>some<\/I> kind of animal, you&#8217;re free to provide a lion cage <I>or<\/I> a butterfly cage.<br \/>\n<P><\/P><br \/>\n<BLOCKQUOTE><br \/>\n<HR><br \/>\n<B>Note:<\/B> It&#8217;s also possible to specify a <I>lower bound<\/I> by using the <CODE>super<\/CODE> keyword instead of <CODE>extends<\/CODE>. The code <CODE>&lt;? super Animal&gt;<\/CODE>, therefore, would be read as &#8220;an unknown type that is a supertype of <CODE>Animal<\/CODE>, possibly <CODE>Animal<\/CODE> itself&#8221;. You can also specify an unknown type with an <I>unbounded wilcard<\/I>, which simply looks like <CODE>&lt;?&gt;<\/CODE>. An unbounded wildcard is essentially the same as saying <CODE>&lt;? extends Object&gt;<\/CODE>.<br \/>\n<HR><br \/>\n<\/BLOCKQUOTE><br \/>\n<P>While <CODE>Cage&lt;Lion&gt;<\/CODE> and <CODE>Cage&lt;Butterfly&gt;<\/CODE> are not subtypes of <CODE>Cage&lt;Animal&gt;<\/CODE>, they are in fact subtypes of <CODE>Cage&lt;? extends Animal&gt;<\/CODE>: <\/P><PRE>\tsomeCage = lionCage; \/\/ OK<br \/>\n\tsomeCage = butterflyCage; \/\/ OK<br \/>\n<\/PRE>So now the question becomes, &#8220;Can you add butterflies and lions directly to <CODE>someCage<\/CODE>?&#8221;. As you can probably guess, the answer to this question is &#8220;no&#8221;. <PRE>\tsomeCage.add(king);\t\/\/ compiler-time error<br \/>\n\tsomeCage.add(monarch);\t\/\/ compiler-time error<br \/>\n<\/PRE>If <CODE>someCage<\/CODE> is a butterfly cage, it would hold butterflies just fine, but the lions would be able to break free. If it&#8217;s a lion cage, then all would be well with the lions, but the butterflies would fly away. So if you can&#8217;t put anything at all into <CODE>someCage<\/CODE>, is it useless? No, because you can still read its contents: <PRE>\tvoid feedAnimals(Cage&lt;? extends Animal&gt; someCage) {<br \/>\n\t    for (Animal a : someCage)<br \/>\n\t\ta.feedMe();<br \/>\n\t}<br \/>\n<\/PRE>Therefore, you could house your animals in their individual cages, as shown earlier, and invoke this method first for the lions and then for the butterflies: <PRE>\tfeedAnimals(lionCage);<br \/>\n\tfeedAnimals(butterflyCage);<br \/>\n<\/PRE>Or, you could choose to combine your animals in the all-animal cage instead: <PRE>\tfeedAnimals(animalCage);<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wildcards Earlier we mentioned that English is ambiguous. The phrase &#8220;animal cage&#8221; can reasonably mean &#8220;all-animal cage&#8221;, but it also suggests an entirely different concept: a cage designed not for any kind of animal, but rather for some kind of animal whose type is unknown. In generics, an unknown type is represented by the wildcard &hellip; <a href=\"https:\/\/www.strongd.net\/?p=415\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Wildcards<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-415","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=415"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/415\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}