{"id":2622,"date":"2020-04-21T00:44:20","date_gmt":"2020-04-20T19:14:20","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=2622"},"modified":"2020-04-21T00:44:20","modified_gmt":"2020-04-20T19:14:20","slug":"generics-in-java","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/generics-in-java\/","title":{"rendered":"Generics in Java"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Generics deal with type-safe objects making the code stable by detecting the bug at compile time.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Before Generics was introduced, we can store any type of data in the collection. Now, in Generics, we can store only a specific type of object.<\/span><\/p>\n<h2><b>Advantages of Generics:<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">1] Type-Safety: We can now store only a specific type of object. We cannot store other objects in the collection.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">2] We do not need to typecast the object, so Typecasting is not required.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">3] It can detect the errors at compile time itself, and hence the problem does not occur at run time.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">4] Type Erasure: Any extra information added using Generics into source code will get removed as soon as its byte code is generated.<\/span><\/p>\n<h3><b>Type Parameters:<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">The standard type parameters are:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">T \u2013 Type<\/span><\/p>\n<p><span style=\"font-weight: 400;\">K \u2013 Key<\/span><\/p>\n<p><span style=\"font-weight: 400;\">E \u2013 Element<\/span><\/p>\n<p><span style=\"font-weight: 400;\">V \u2013 Value<\/span><\/p>\n<p><span style=\"font-weight: 400;\">N \u2013 Number<\/span><\/p>\n<h3><b>Generic Class:<\/b><\/h3>\n<p><span style=\"font-weight: 400;\">When more than one type of parameter is declared in a class, it is said to be as Generic <a href=\"https:\/\/www.h2kinfosys.com\/blog\/design-a-class-in-java\/\">Class<\/a>.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">class DemoClass\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">{<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0private Object t;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0public void set(Object t)\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">{\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">this.t = t;\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0public Object get()\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">{<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0return t;\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}<\/span><\/pre>\n<h3><b>Generic Method: <\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Generic Method is the same as Generic Class, where more than one type of parameter can be declared. The only difference between the Generic method and Generic class is that the scope of a type parameter resides within its method.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">public static &lt;T&gt; int countAllOccurrences(T[] list, T item) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0int count = 0;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0if (item == null) {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for ( T listItem : list )<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (listItem == null)<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0count++;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0else {<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for ( T listItem : list )<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (item.equals(listItem))<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0count++;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0}<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0return count;<\/span>\r\n\r\n<span style=\"font-weight: 400;\">} \u00a0 <\/span><\/pre>\n<h3><b>Generics With Wildcards: <\/b><\/h3>\n<p><span style=\"font-weight: 400;\">The ? (question mark) denotes the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/java-language-packages\/\">wildcard<\/a>. It can be used as a type of a parameter, a<a href=\"https:\/\/www.h2kinfosys.com\/blog\/java-variables-and-data-types\/\"> local variable<\/a>, field or return type. Examples of wildcard parameterized types includes Collection&lt;?&lt;, List&lt;? extends Number&lt;, Comparator&lt;? super String&gt; and Pair&lt;String,?&gt;. Wildcards can be either bounded or unbounded.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">abstract class Shaper{\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">abstract void draw();\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">class Triangle extends Shaper{\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">void draw(){System.out.println(\"drawing Triangle\");}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">class Square extends Shaper{\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">void draw(){System.out.println(\"drawing Square\");}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">class GenericTest{\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">public static void drawShapers(List&lt;? extends Shaper&gt; lists){\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">for(Shaper s:lists){\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">s.draw();\/\/calling method of Shaper class by child class instance\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">public static void main(String args[]){\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">List&lt;Triangle&gt; list1=new ArrayList&lt;Triangle&gt;();\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">list1.add(new Triangle());\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">List&lt;Square&gt; list2=new ArrayList&lt;Square&gt;();\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">list2.add(new Square());\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">list2.add(new Square());\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">drawShapers(list1);\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">drawShapers(list2);\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}}\u00a0 <\/span><\/pre>\n<h3><b>Bounded Wildcards: <\/b><\/h3>\n<p><span style=\"font-weight: 400;\">Bounded wildcards have some restrictions on type parameters. This restriction can be enforced using super and extends keyword.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>1] Upper Bounded Wildcards:<\/strong> This is used to decrease the restriction on a variable. It is declared by using wildcard character following by extends or implements and then following by its upper bound.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">public class UpperBoundWildcard {\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0private static Double add(ArrayList&lt;? extends Number&gt; num) {\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0double sum=0.0;\u00a0\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(Number n:num)\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sum = sum+n.doubleValue();\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return sum;\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public static void main(String[] args) {\u00a0\u00a0\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ArrayList&lt;Integer&gt; l1=new ArrayList&lt;Integer&gt;();\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0l1.add(10);\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0l1.add(20);\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"displaying the sum= \"+add(l1));\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ArrayList&lt;Double&gt; l2=new ArrayList&lt;Double&gt;();\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0l2.add(30.0);\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0l2.add(40.0);\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"displaying the sum= \"+add(l2));\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">} <\/span><\/pre>\n<p><span style=\"font-weight: 400;\"><strong>2] Lower Bounded Wildcards:<\/strong> This is used to decrease the restriction on <\/span><span style=\"font-weight: 400;\">the unknown type to be a specific type or a supertype<\/span><span style=\"font-weight: 400;\">. It is declared by using wildcard character following by super keyword and then following by it lower bound.<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">public class LowerBoundWildcard {\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public static void addNumbers(List&lt;? super Integer&gt; list) {\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(Object n:list)\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(n);\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">public static void main(String[] args) {\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0List&lt;Integer&gt; l1=Arrays.asList(1,2,3);\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"displaying the Integer values\");\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0addNumbers(l1);\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0List&lt;Number&gt; l2=Arrays.asList(1.0,2.0,3.0);\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"displaying the Number values\");\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0addNumbers(l2);\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}\u00a0 <\/span><\/pre>\n<h3><b>Unbounded Wildcards: <\/b><\/h3>\n<p><span style=\"font-weight: 400;\">It represents the list of an unknown type say for eg. List&lt;?&gt;. Unbounded wildcards are useful in two cases:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">When the method is implemented in the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/object-oriented-programming-concepts\/\">Object Class<\/a>.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">When Generic Class contains those methods which do not depend on the type parameter.<\/span><\/li>\n<\/ol>\n<pre><span style=\"font-weight: 400;\">public class UnboundedWildcard {\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public static void display(List&lt;?&gt; list)\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0{\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for(Object o:list)\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(o);\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0public static void main(String[] args) {\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0List&lt;Integer&gt; l1=Arrays.asList(1,2,3);\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0System.out.println(\"displaying the Integer values\");\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0display(l1);\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0List&lt;String&gt; l2=Arrays.asList(\"One\",\"Two\",\"Three\");\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(\"displaying the String values\");\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0display(l2);\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">\u00a0\u00a0\u00a0\u00a0}\u00a0\u00a0<\/span>\r\n\r\n<span style=\"font-weight: 400;\">}\u00a0 <\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generics deal with type-safe objects making the code stable by detecting the bug at compile time. Before Generics was introduced, we can store any type of data in the collection. Now, in Generics, we can store only a specific type of object. Advantages of Generics: 1] Type-Safety: We can now store only a specific type [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2655,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[398,533,536,535,534],"class_list":["post-2622","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-class","tag-generics-in-java","tag-lower-bounded-wildcards","tag-upper-bounded-wildcards","tag-wildcards"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=2622"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2622\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/2655"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=2622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=2622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=2622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}