Generic class in general – A class that can refer to any type is known as generic class.

To understand Generic classes, lets have a look on generic and nongeneric version of ArrayList Class:

Generics are introduced in java 1.5, it means until java 1.4 ArrayList class is nongeneric.

 java 1.4 non generic version of Arraylist looks like below

add(Object o) method can take object as the argument and hence we can add any type of object to the ArrayList class. Due to this we are not getting type safety.

The return type of get(int index) method is object hence at the time of retrieval compulsory we should perform type casting.

Since java 1.5 generic version of Arraylist looks like below

based on our requirement T will be replaced with our provided parameter type. For Example
To hold only Integer type of objects we can create ArrayList object as follows.

ArrayList<Integer> l=new ArrayList<Integer>();

In this case compiler considered ArrayList class is like below.

Now Creating our own generic Class

Testing class:

Console Output:

To Understand about what is generics in java see : Java Generics Inroduction