What is Generics?

Till now whatever classes/methods/constructors/variables we have created were datatype specific i.e. having a fixed datatype which was decided by the owner of the class and cannot be changed once the class is compiled. For example,

public void disp(int x)
{
}

Above method takes an int value as a parameter which do not support any other datatype. But let’s say now we are considering a scenario where by there are 2 developers: 1) developer1 and 2) developer2.

Developer1 has written following class which is used by developer2 to find the maximum number from the given set of numbers as discussed below:

Developer1 Wrote:

public class findMax
{
	public int maxFrom(int[] arr)
	{	
		int max = 0;
		//code to find max out of given arr and assing it to max variable
		return max;
	
	}
}

Now, Developer2 wants to use the class which is created by developer1. Remember that developer2 cannot modify the class findMax which is created by developer1.
Developer2 Wrote:

public class abc
{
	public static void main(String[] args)
	{	
		int[] iarr = {1,2,3,4,5,6,7,8,9,0};
		findMax fm = new findMax();
		int max = fm.maxFrom(iarr);
	}
}

Correct! Developer2 can use it easily by creating an object and doing a method call.
Then what is the problem? Problem is when the developer2 want to find max number out of given set of float numbers. He is planning to pass float[] as an argument and wants a float value back. But there is no such method available in the class findMax which is developed by developer1.

Let’s say on special request of developer2 even if developer1 agrees to add a overloaded method then that can be said as a temporary solution!
Developer1 Wrote:

public class findMax
{
	public int maxFrom(int[] arr)
	{	
		int max = 0;
		//code to find max out of given arr and assing it to max variable
		return max;
	
	}
	public float maxFrom(float[] arr)
	{	
		float max = 0;
		//code to find max out of given arr and assing it to max variable
		return max;
	
	}

}

It’s a temporary solution because it cannot be said that there won’t be any future requirement (of developer2) to find a maximum double value out of the given double[]. In that case we have to write one more overloaded method! But Howmany? Whatever number of overloaded method developer1 writes are fixed while writing the class. So, developer2 will be always dependent on developer1! So, This is the problem which can be resolved by the use of Generics…

Generics

Generics is a Java feature that was introduced with Java SE 5.0. It’s an easy to use feature and many resources are available for Generics but lot many developers face problem in understanding generics concept in java. Anyway, Let’s start!

Variable:
A variable is normally used by a developer to store some literals right? But lets say a variable which holds the java class/wrapper class that can be said as a generic variable.

How developer1 can write a Generic calss “findMax”?
Developer1 Wrote:

public class findMax<T>
{
	public T maxFrom(T[] arr)
	{	
		T max = 0;
		//code to find max out of given arr and assing it to max variable
		return max;
	
	}
}

So, As shown in the code above our method maxForm() not takes T type of array as an argument and returns T type of data. Here “T” is the variable which will hold the datatype and that can be decided by develope2! How?

Developer2 Wrote:

public class abc
{
	public static void main(String[] args)
	{	
		Integer[] iarr = {1,2,3,4,5,6,7,8,9,0};
		findMax<Integer> fm = new findMax<Integer>();
		int max = fm.maxFrom(iarr);
		System.out.println(max);
	}
}

Note: Make sure you do not pass any datatype as a generic value, Which is not allowed. Here I passed Integer instead of int whereby Integer is the wrapper class of int datatype.

Full Program by Developer1:

import java.util.*;
class findMax<T>
{
	public T maxFrom(T[] arr)
	{	
		T max = arr[0];

		for(int i=0;i<arr.length-1;i++)
		{
			Comparable one = (Comparable)arr[i];
			Comparable two = (Comparable)arr[i+1];

			if(one.compareTo(two) < 0)
			{
				max = arr[i+1];
			}	
		}
		//code to find max out of given arr and assing it to max variable
		return max;
	
	}
}

Developer2 may use it with different different datatypes by using appropriate wrapper class.

public class abc
{
	public static void main(String[] args)
	{	
		Integer[] iarr = {1,2,3,4,5,6,7,8,9,0};
		findMax<Integer> fm = new findMax<Integer>();
		int max = fm.maxFrom(iarr);
		System.out.println(max);

		Float[] farr = {1.2,2.3,3.4,4.5,5.6,6.9,7.2,8.1,9.0,0.2};
		findMax<Float> ffm = new findMax<Float>();
		float fmax = ffm.maxFrom(farr);
		System.out.println(fmax);

	}
}

Again, A major change that you will find is: Comparable? What is that? Ammm… Comparable is an interface which belongs to java.util package which can be used to compare any object. If we need to compare a normal variable, we might have used <, >, == operators but in our case developer1 (who is writing the logic) does not know anything about what kind of value T will hold. So, We are casting our array indexes to Comparable and comparing it so that we can find the maximum value.

Hope you have understood what is Generics! Write a comment or ask a question. Feel free to contact me.