Generic Classes

Generic Classes

Until 1.4 version we have ArrayList class with the declarations as follows.

class ArrayList {
   add(Object o);
   Object get(int index);
}

add method contain Object as argument, hence we can add any kind of object.

Β 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.

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);
}

Based on run time requirement the corresponding version of ArrayList will loaded.

ArrayList<String> list = new ArrayList<String>();

For the following declarations the corresponding loaded class is

class ArrayList<String> {
   add(String);
   String get(int);
}

The argument to the add method is String hence we should add only String Object as the result we will get type safety. The return type of get() method is String. Hence at the time of retrieval no need to perform typecasting.

We can define our own generic classes also

class Gen<T> {
   T obj;

   Gen(T obj) {
      this.obj = obj;
   }

   public void show() {
      System.out.println("The type of Object is : " + obj.getClass().getName());
   }

   public T getObj() {
      return obj;
   }
}

public class MyGenerics {
   public static void main(String[] args) {
      Gen<String> gen1 = new Gen<>("Ashok");
      gen1.show();
      System.out.println(gen1.getObj());
      Gen<Integer> gen2 = new gen<>(10);
      gen2.show();
      System.out.println(gen2.getObj());
   }
}

Output
The type of Object is : java.lang.String
Ashok
The type of Object is : java.lang.Integer
10
Bounded Types

We can bound the type parameter for a particular range. Such type of types is called bounded types. We can achieve this by using extends keyword.

E.g

class Gen<T> {
}

Here we can pass any types as the type parameter and there are no restrictions.

Gen<String> g1 = new Gen<String>();
Gen<Integer> g2 = new Gen<Integer>();

Class Gen<T extends X>

If β€˜X’ is a class then any type which is the child class of β€˜X’ is allowed as the type parameter.

If β€˜X’ is an interface then any type which is the implementation class of β€˜X’ is allowed as the type parameter.

E.g

class Gen<T extends Number> {

}

In this case as the type parameter we can take either number or it’s child classes.

Gen<Integer> g1 = new Gen<Integer>();// Fine
Gen<String> g2 = new Gen<String>(); // C.E

Because type parameter String is not with in it’s bound

class Gen<T extends Runnable> {

}

In this case as the type parameter we can take any implementation class of Runnable interface.

Gen<Thread> t1 = new Gen<Thread>(); // Fine
Gen<String> t2 = new Gen<String>(); // C.E

Because String is not implementation class of Runnable interface.

In generics we have only extends keyword and there is no implements keyword. It’s purpose is also survived by using extends keyword only.

class Gen<T extends Number> {
   T obj;
   Gen(T obj) {
     this.obj = obj;
   }
   void show() {
      System.out.println("The int value is :" + ob.intValue());
   }
}
public class MyGeneric {
   public static void main(String arg[]) {
      Gen<Integer> t1 = new Gen<Integer>(new Integer(10));
      t1.show();
      Gen<Double> t2 = new Gen<Double>(10.5);
      t2.show();
      Gen<String> t3 = new Gen<String>("Ashok"); // C.E
      t3.show();
   }
}

Generic Classes
Scroll to top