{"id":1878,"date":"2018-12-20T12:00:00","date_gmt":"2018-12-20T11:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/generic-classes-in-java\/"},"modified":"2026-07-05T03:21:49","modified_gmt":"2026-07-05T01:21:49","slug":"generic-classes-in-java","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/generic-classes-in-java\/","title":{"rendered":"Generic Classes 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>Today we are going to be looking at generic classes in Java. This is continuation of our Tutorials on Generics in Java.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-254 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Generic-Classes-in-Java-300x150.jpg\" alt=\"\" width=\"300\" height=\"150\" \/><\/p>\n<p>We can also create generic classes in Java. In this case, the type of object that can be instantiated with this class is not known at design time. The return type is specified at run time.<\/p>\n<p>A generic class declaration in Java is similar to normal classes with the exception that the the generic class name is followed by a type parameter section. The type parameter section can have one or more type parameters which are separated by commas.<\/p>\n<p>Classes created in this way are known as parameterized classes and can accept one or more parameters.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>In this example, we define a generic class. Then we create objects based on the generic class and call the methods of the generic class.<\/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: #888888;\">\/*<\/span>\n<span style=\"color: #888888;\"> * Generic class with parameter T<\/span>\n<span style=\"color: #888888;\"> * Writen by: Kindson The Genuis<\/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;\">Box<\/span><span style=\"color: #333333;\">&lt;<\/span>T<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> T t<span style=\"color: #333333;\">;<\/span>\t\t<span style=\"color: #888888;\">\/\/Instance variable of type T<\/span>\n\t\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Box<\/span><span style=\"color: #333333;\">(<\/span>T t<span style=\"color: #333333;\">)<\/span>\t\t<span style=\"color: #888888;\">\/\/Generic constructor take type T<\/span>\n\t<span style=\"color: #333333;\">{<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">t<\/span> <span style=\"color: #333333;\">=<\/span> t<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #333333;\">}<\/span>\n\t\n\t<span style=\"color: #008800; font-weight: bold;\">public<\/span> T <span style=\"color: #0066bb; font-weight: bold;\">get<\/span><span style=\"color: #333333;\">()<\/span>\t\t<span style=\"color: #888888;\">\/\/Generic method get, returns type T<\/span>\n\t<span style=\"color: #333333;\">{<\/span>\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> t<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.0: Class definition of a generic class<\/p>\n<p>&nbsp;<\/p>\n<p>The class definition of a generic class is presented in Listing 1.0. In this code, we can see that this class has a constructor that take a parameter of type T which would be specified when the object is created.<\/p>\n<p><strong>Example: Using a Generic Class<\/strong><\/p>\n<p>Copy the code below to your IDE and run it and view\u00a0 the output.<\/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: #888888;\">\/*<\/span>\n<span style=\"color: #888888;\"> * Creating and calling methods of a generic class<\/span>\n<span style=\"color: #888888;\"> * Writen by: Kindson The Genuis<\/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;\">TestGeneric<\/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\t<span style=\"color: #888888;\">\/\/ TODO Auto-generated method stub<\/span>\n\t\t\n\t\tBox<span style=\"color: #333333;\">&lt;<\/span>Integer<span style=\"color: #333333;\">&gt;<\/span> intBox <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Box<span style=\"color: #333333;\">&lt;<\/span>Integer<span style=\"color: #333333;\">&gt;(<\/span><span style=\"color: #0000dd; font-weight: bold;\">20<\/span><span style=\"color: #333333;\">);<\/span>\n\t\tBox<span style=\"color: #333333;\">&lt;<\/span>String<span style=\"color: #333333;\">&gt;<\/span> strBox <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Box<span style=\"color: #333333;\">&lt;<\/span>String<span style=\"color: #333333;\">&gt;(<\/span><span style=\"background-color: #fff0f0;\">\"Generic Tutorial!\"<\/span><span style=\"color: #333333;\">);<\/span>\n\t\tBox<span style=\"color: #333333;\">&lt;<\/span>Character<span style=\"color: #333333;\">&gt;<\/span> charBox <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Box<span style=\"color: #333333;\">&lt;<\/span>Character<span style=\"color: #333333;\">&gt;(<\/span><span style=\"color: #0044dd;\">'K'<\/span><span style=\"color: #333333;\">);<\/span>\n\t\tBox<span style=\"color: #333333;\">&lt;<\/span>Float<span style=\"color: #333333;\">&gt;<\/span> floBox <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">new<\/span> Box<span style=\"color: #333333;\">&lt;<\/span>Float<span style=\"color: #333333;\">&gt;(<\/span><span style=\"color: #6600ee; font-weight: bold;\">45.04f<\/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;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Integer value: \"<\/span> <span style=\"color: #333333;\">+<\/span> intBox<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">get<\/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;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"String value: \"<\/span> <span style=\"color: #333333;\">+<\/span> strBox<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">get<\/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;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Character value: \"<\/span> <span style=\"color: #333333;\">+<\/span> charBox<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">get<\/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;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"Float value: \"<\/span> <span style=\"color: #333333;\">+<\/span> floBox<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">get<\/span><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: Using a generic class<\/p>\n<p>&nbsp;<\/p>\n<p>If you are using Eclipse IDE, then your output would be like shown in Figure 1.0<\/p>\n<figure id=\"attachment_253\" aria-describedby=\"caption-attachment-253\" style=\"width: 725px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-253 \" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Output-of-Generic-Class2.jpg\" alt=\"\" width=\"725\" height=\"439\" \/><figcaption id=\"caption-attachment-253\" class=\"wp-caption-text\">Figure 1.0<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\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 Today &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-1878","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1878","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=1878"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1878\/revisions"}],"predecessor-version":[{"id":2046,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1878\/revisions\/2046"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}