Generics In Java

The concept of Generics in Java is completely similar to that of Templates in C++. So if you know Templates it would be a lot easier for you to understand Generics, if you don’t, then don’t worry just go through this article and you would be good to go ahead.

Till now you would have worked with data structures such as Arrays, Queues, Linked-List, Binary Trees, Binary Search Trees, etc. In using the data structures such as Arrays, Linked Lists and Queues there was one limitation that we if we had made a class and an object of these data structures for a specific data type we could use only that class and that object for data type only. But if we wanted to use a new data type we had to repeat the whole process, again and again, every time we wanted to use it for a different data type. That thing would have been a lot cumbersome, would have taken a lot more time and space also.

So here comes the concept of Generics in Java. Generics in itself specifies what it is for, that is we make a general class for all the data types that we might want to use, these data types can be primitive or non-primitive also. So we make a general type of a class and say we make it for a type T and do everything according to this T and at the time of execution whatever data type we want to make that object and class for we just pass that data type like integer, string, etc. and this data type, goes on replacing all the places where T is and therefor runs for that specific data type. We can pass more than one data types also at a time, in that case, we just need to do it for T, U, V.etc. , depending upon the number of data types you want to use that object with. This concept can be applied with methods also in that class.

Examples uses of Generics in Java are:-

We can use them in ArrayList class, but you can use any collection class such as ArrayList, LinkedList, HashSet, TreeSet, HashMap, Comparator etc.

We use <> to specify the parameter types in Generic class creation.

To create an object of a class , the syntax we use is :

BaseType<Type>Obj = new BaseType <Type>();

Let’s understand with the help of a Code:

// A Simple Java program to show working of user defined

// < > to specify Parameter type

class Test<T>

{

//object of type T

T obj;

// constructor

Test(T obj)

{

this.obj = obj;

}

public T getObject() { return this.obj; }

}

// Driver class

class Main

{

public static void main (String[] args)

{

// instance of Integer type

Test <Integer> obj1= new Test<Integer>(22);

System.out.println(obj1.getObject());

// instance of String type

Test <String> Obj2 = new Test<String>("AnubhavMishra");

System.out.println(sObj.getObject());

}

}

Output

22

AnubhavMishra

This program was for only one parameter but as I told you can do it for more than one parameter too, So lets Code now for let’s say 2 parameter U, V:

class Test<T, U>

{

T obj1; // An object of type T

U obj2; // An object of type U

// constructor

Test(T obj1, U obj2)

{

this.obj1 = obj1;

this.obj2 = obj2;

}

// To print objects of T and U

public void print()

{

System.out.println(obj1);

System.out.println(obj2);

}

}

// Driver class

class Main

{

public static void main (String[] args)

{

Test <String, Integer> obj =

new Test<String, Integer>("AM", 22);

obj.print();

}

}

Output

AM

22

We can also make Generic functions, but I felt that it’s a bit too much for a beginner. So I am not including it here but for all of you who want to know about, you can check the references.

Finally let’s take a look at all of the Advantages that Generics provides:-

  1. Code Reusability — Write once use for any number of data types.

So wrapping up in a nutshell, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs. The difference is that the inputs to formal parameters are values, while the inputs to type parameters are types.

Do give this article a clap if you like it and you can also follow me on my social media handles, you can check them from my profile.

References