
Generics is a way in which a single method can be called with arguments of different types i.e; different classes. For example if a method has only one argument, if we use generics on that argument then we can pass objects of any class as a parameter to that method. How we can do that we will show you below.
According to the parameter which was passed, the method will handle the execution appropriately.
Also if it is generic class, it can work with Objects with different classes. Let’s see how a generic class is created.
class GenericTest<T>
{
// An object of type T is declared
T obj;
//We declare the constructor below
GenericTest(T obj)
{
this.obj = obj;
}
//We fetch the object which was passed during invoking the constructor
public T getObject() {
return this.obj;
}
}
If you see above, class GenericTest called with generic type <T>.
Now let’s call the class or you can say instantiate the class object.
class Main
{
public static void main (String[] args)
{
GenericTest<String> genObject =
new GenericTest<String>("John");
System.out.println(genObject.getObject());
GenericTest<Integer> genObject =
new GenericTest<Integer>(100);
System.out.println(genObject.getObject());
}
}
If you see above, the class GenericTest is called with an String Type and then with an Integer Type.
The output is printed as:
John
100
So the generic class GenericTest can handle any type and give the output accordingly.
Now let’s take a look into a generic method.
class Test
{
public <T> void genericMethod (T obj)
{
System.out.println(obj.getClass().getName() +
" = " + obj);
}
public static void main(String[] args)
{
genericMethod(100);
genericMethod("John");
}
}
O/P:
Java.Lang.Integer = 100
Java.Lang.String = John
So the method handled both String and Integer types and gave the appropriate output. We can add additional types as well like double, BigDecimal etc.
***Remember one thing. Generics applies only for objects/reference types and not for primitives ****
Below line will give an error.
GenericTest<float> obj = new
Generic
Test
<float>(20.0f);
Taking an example of collection classes we can have a generic class or generic method having generic type as List. By doing this, we can pass all it’s child classes like ArrayList, LinkedList, Vector etc to that method or Class and that should work perfectly.
But now let’s look at a problem which can occur if generics are not used.
Class GenericTest
{
public List populateList()
{
ArrayList list = new ArrayList();\\Line 5
list.add("James");//Line 7
list.add("Gosling");//Line 8
list.add("Gosling2"); //Line 9
list.add(100); //Line 10
return list;
}
public Static void main(Strng args[])
{
GenericTest genTest = new GenericTest();
ArrayList arrList = genTest.populateList();
for(String val : arrList) //Line 20
System.out.println("The List value is "+val);
}
}
Â
Line 10, would not give any issue during compilation. But line 20, would fail during run time because we are mapping an integer 100 to a String variable.
To resolve this, we should change the ArrayList declaration in Line 5 to below
ArrayList<String> arrList = new ArrayList<String>();
By doing this, while inserting 100 an Integer as seen in line 10 above, it would give an compile time error.
Benefits of Generic:
1)Type safety as seen above. String type would only include String values.
2)For one argument type, we can pass any child types against it. Like for type List, we can pass arguments with the types ArrayList,LinkedList etc.