{"id":413,"date":"2007-10-21T00:00:00","date_gmt":"2007-10-21T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=413"},"modified":"2007-10-21T00:00:00","modified_gmt":"2007-10-21T00:00:00","slug":"Generic Bounded Type Parameters","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=413","title":{"rendered":"Generic Bounded Type Parameters"},"content":{"rendered":"<p><DIV id=PageTitle>Bounded Type Parameters<\/DIV><br \/>\n<BLOCKQUOTE>There may be times when you&#8217;ll want to restrict the kinds of types that are allowed to be passed to a type parameter. For example, a method that operates on numbers might only want to accept instances of <CODE>Number<\/CODE> or its subclasses. This is what <I>bounded type parameters<\/I> are for.<br \/>\n<P>To declare a bounded type parameter, list the type parameter&#8217;s name, followed by the <CODE>extends<\/CODE> keyword, followed by its <I>upper bound<\/I>, which in this example is <CODE>Number<\/CODE>. Note that, in this context, <CODE>extends<\/CODE> is used in a general sense to mean either &#8220;extends&#8221; (as in classes) or &#8220;implements&#8221; (as in interfaces). <\/P><br \/>\n<P><\/P><br \/>\n<BLOCKQUOTE><PRE>\/**<br \/>\n * This version introduces a bounded type parameter.<br \/>\n *\/<br \/>\npublic class Box&lt;T&gt; {<\/p>\n<p>    private T t;          <\/p>\n<p>    public void add(T t) {<br \/>\n        this.t = t;<br \/>\n    }<\/p>\n<p>    public T get() {<br \/>\n        return t;<br \/>\n    }<\/p>\n<p>    public &lt;U <B>extends Number<\/B>&gt; void inspect(U u){<br \/>\n        System.out.println(&#8220;T: &#8221; + t.getClass().getName());<br \/>\n        System.out.println(&#8220;U: &#8221; + u.getClass().getName());<br \/>\n    }<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        Box&lt;Integer&gt; integerBox = new Box&lt;Integer&gt;();<br \/>\n        integerBox.add(new Integer(10));<br \/>\n        integerBox.inspect(&#8220;some text&#8221;); <B>\/\/ error: this is still String!<\/B><br \/>\n    }<br \/>\n}<br \/>\n<\/PRE><\/BLOCKQUOTE>By modifying our generic method to include this bounded type parameter, compilation will now fail, since our invocation of <CODE>inspect<\/CODE> still includes a <CODE>String<\/CODE>:<br \/>\n<BLOCKQUOTE><PRE>Box.java:21: &lt;U&gt;inspect(U) in Box&lt;java.lang.Integer&gt; cannot<br \/>\n  be applied to (java.lang.String)<br \/>\n                        integerBox.inspect(&#8220;10&#8221;);<br \/>\n                                  ^<br \/>\n1 error<br \/>\n<\/PRE><\/BLOCKQUOTE><br \/>\n<P>To specify additional interfaces that must be implemented, use the <CODE>&amp;<\/CODE> character, as in: <BR><\/P><br \/>\n<BLOCKQUOTE><PRE>&lt;U extends Number &amp; MyInterface&gt;<br \/>\n<\/PRE><\/BLOCKQUOTE><\/BLOCKQUOTE><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bounded Type Parameters There may be times when you&#8217;ll want to restrict the kinds of types that are allowed to be passed to a type parameter. For example, a method that operates on numbers might only want to accept instances of Number or its subclasses. This is what bounded type parameters are for. To declare &hellip; <a href=\"https:\/\/www.strongd.net\/?p=413\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Generic Bounded Type Parameters<\/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-413","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/413","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=413"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/413\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}