1. Until 1.4 version we have ArrayList class with the declarations as follows.
    class ArrayList{
    	add(Object o);
    	Object get(int index);
  2. add() method contain Object as argument, hence we can add any kind of object.
  3. As a result we can’t get any type safety. The return type of get() method is object hence at the time of retrieval we should perform typecasting.
  4. But in the 1.5 version we have generic ArrayList class with the definition as follows.
    class ArrayList<T>{
      add(T t);
      T get(int);
  5. Based on run time requirement the corresponding version of ArrayList will loaded.
    ArrayList<String> l = new ArrayList<String>();</li>
  6. For the following declarations the corresponding loaded class is
    class ArrayList<String>{
      String get(int);
  7. The argument to the add method is String hence we should add only String Object as the result we will get type safety.
  8. The return type of get() method is String. Hence at the time of retrieval no need to perform typecasting.
  9. We can define our own generic classes also.
    class GenClass<T> {
    	T ob;
    	GenClass(T ob) {
    		this.ob = ob;
    	public void show() {
    		System.out.println("The type of Object is : " + ob.getClass().getName());
    	public T getOb() {
    		return ob;
    class GenericDemo {
    	public static void main(String[] args) {
    		GenClass<String> g1 = new GenClass<String>("Java");
    		GenClass<Integer> g2 = new GenClass<Integer>(10);
    The type of Object is : java.lang.String
    The type of Object is : java.lang.Integer