{"id":1877,"date":"2018-12-20T12:00:00","date_gmt":"2018-12-20T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/more-on-java-generics-bounded-generics\/"},"modified":"2026-07-05T03:21:46","modified_gmt":"2026-07-05T01:21:46","slug":"more-on-java-generics-bounded-generics","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/more-on-java-generics-bounded-generics\/","title":{"rendered":"Bounded Generics in Java"},"content":{"rendered":"<p><strong>Other Java Topics<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/interfaces-in-java-basics-of-java-interface\/\">Interfaces in Java<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/threading-and-multithreading-in-java\/\">Threading and Multithreading in Java<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/generics-in-java-generic-classes-and-methods-in-java\/\">Introduction to Generics in Java<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/more-on-java-generics-bounded-generics\/\">Bounded Generics in Java<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/generic-classes-in-java\/\">Generic Classes in Java<\/a><\/li>\n<\/ul>\n<p>This is a continuation of our tutorials on Java Generics.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-249 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Bounded-Generics-in-Java-300x164.jpg\" alt=\"\" width=\"300\" height=\"164\" \/><\/p>\n<p>As you know, Generics in Java allows for data types to be defined as a generic type in design time and the actual datatype is determined at run time.<\/p>\n<p>In case of bounded generic type, we\u00a0 would wan to restrict the data types tat can be used in the type parameter &lt;T&gt; certain data types. For instance, we may want to restrict the type to Number-related data types i.e classes derived from Number class.\u00a0 How do we do that?<\/p>\n<p>One way to achieved this is by specifying the class name along with the generic parameter declarations. In this case, then the generic type is restricted to only the class specified and the classes derived from it.<\/p>\n<p>The syntax for this is:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"> <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333333;\">&lt;<\/span>T <span style=\"color: #008800; font-weight: bold;\">extends<\/span> Comparable<span style=\"color: #333333;\">&lt;<\/span>T<span style=\"color: #333333;\">&gt;&gt;<\/span> T GetMax<span style=\"color: #333333;\">(<\/span>T x<span style=\"color: #333333;\">,<\/span> T y<span style=\"color: #333333;\">,<\/span> T z<span style=\"color: #333333;\">)<\/span>\n <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #888888;\">\/\/Body of the method<\/span>\n <span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<\/div>\n<p>Listing 1.0: Bounded generic type declaration<\/p>\n<p><strong>How it Works<\/strong><\/p>\n<ul>\n<li>The first T, specifies the generic type.<\/li>\n<li>Comparable refers to the class such that the generic type is restricted to object of this type and classed derived from it.<\/li>\n<li>The second T is the return type<\/li>\n<li>GetMax is the name of the method<\/li>\n<li>T x, T y and T z specifies the types for the argument to the function<\/li>\n<\/ul>\n<p>We now present the complete program below.<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/*<\/span>\n<span style=\"color: #888888;\"> * Program to demonstrated bounded generics<\/span>\n<span style=\"color: #888888;\"> * by: Kindson The Genius<\/span>\n<span style=\"color: #888888;\"> *\/<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">GenericTut<\/span> <span style=\"color: #333333;\">{<\/span>\n\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">printf<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Max is for integers: %d \\n\"<\/span><span style=\"color: #333333;\">,<\/span> GetMax<span style=\"color: #333333;\">(<\/span><span style=\"color: #0000dd; font-weight: bold;\">6<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">9<\/span><span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">);<\/span>\n\t\t\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">printf<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Max is strings is %s \\n\"<\/span> <span style=\"color: #333333;\">,<\/span> GetMax<span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Elephant\"<\/span><span style=\"color: #333333;\">,<\/span><span style=\"background-color: #fff0f0;\">\"Cat\"<\/span><span style=\"color: #333333;\">,<\/span> <span style=\"background-color: #fff0f0;\">\"Monkey\"<\/span><span style=\"color: #333333;\">));<\/span>\n\t\t\n\t\tSystem<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">printf<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Max for char is %c \\n\"<\/span><span style=\"color: #333333;\">,<\/span> GetMax<span style=\"color: #333333;\">(<\/span><span style=\"color: #0044dd;\">'c'<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0044dd;\">'r'<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0044dd;\">'y'<\/span><span style=\"color: #333333;\">));<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333333;\">&lt;<\/span>T <span style=\"color: #008800; font-weight: bold;\">extends<\/span> Comparable<span style=\"color: #333333;\">&lt;<\/span>T<span style=\"color: #333333;\">&gt;&gt;<\/span> T GetMax<span style=\"color: #333333;\">(<\/span>T x<span style=\"color: #333333;\">,<\/span> T y<span style=\"color: #333333;\">,<\/span> T z<span style=\"color: #333333;\">)<\/span>\n\t<span style=\"color: #333333;\">{<\/span>\n\t\tT max <span style=\"color: #333333;\">=<\/span> x<span style=\"color: #333333;\">;<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>y<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">compareTo<\/span><span style=\"color: #333333;\">(<\/span>max<span style=\"color: #333333;\">)&gt;<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">)<\/span>\n\t\t<span style=\"color: #333333;\">{<\/span>\n\t\t\tmax <span style=\"color: #333333;\">=<\/span> y<span style=\"color: #333333;\">;<\/span>\n\t\t<span style=\"color: #333333;\">}<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>z<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">compareTo<\/span><span style=\"color: #333333;\">(<\/span>max<span style=\"color: #333333;\">)&gt;<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">)<\/span>\n\t\t<span style=\"color: #333333;\">{<\/span>\n\t\t\tmax <span style=\"color: #333333;\">=<\/span> z<span style=\"color: #333333;\">;<\/span>\n\t\t<span style=\"color: #333333;\">}<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> max<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<\/div>\n<p>Listing 1.1: Complete Program on Bounded Generics<\/p>\n<p>Copy this program into your compiler (you can use Netbeans or Eclipse) and make sure you understand how it works.<\/p>\n<p>If you run it correctly, then your output would be like shown in Figure .10<\/p>\n<figure id=\"attachment_248\" aria-describedby=\"caption-attachment-248\" style=\"width: 735px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-248 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Program-Output-for-Bounded-Generics-in-Java-1024x672.jpg\" alt=\"\" width=\"735\" height=\"482\" \/><figcaption id=\"caption-attachment-248\" class=\"wp-caption-text\">Figure 1.0: Program Output for Bounded Generics<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Other Java Topics Interfaces in Java Threading and Multithreading in Java Introduction to Generics in Java Bounded Generics in Java Generic Classes in Java This &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[85],"tags":[],"class_list":["post-1877","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1877"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1877\/revisions"}],"predecessor-version":[{"id":2045,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1877\/revisions\/2045"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}