Types of generics

Generics concept of java allow programmer to write class, interface or method which can work on various data types. Let's take example of stack. The mechanism of stack will be same for every data type. So instead for making a stack class for different data type, generics will be used.


Generics generic means parameterized types.

Parameterized types enable programmer to write class, interface or method in which type of data upon which they operate is specified as parameters.

Generalised class example using Object Reference

Although generalised classes, interfaces and methods can be developed by using reference of Object class. Object class is superclass of all class.


Example :


// Generalised class Example using Object Reference 
class one
{
// reference of Object class

Object a;
one(Object x)
{
a=x;
}

// Print value
void printValue()
{
System.out.println("Value:"+a.toString());
}

// Return object
Object getObject()
{
return a;
}

public static void main(String arg[])
{
Integer a=12;
Character c='A';
one ob=new one(a);
ob.printValue();

// returing object
a=(Integer)ob.getObject(); // Must cast to Integer
ob=new one(c);
ob.printValue();

//return object
c=(Character)ob.getObject(); // must cast to Character
}
}
/*Output:
Value:12
Value:A

In above code for fetching value casting has to be done, otherwise compiler will report an error.


Now if you cast using wrong type then till run time you won't even know what went wrong. At run time when cast exception thrown then we would know what is wrong in the code.

Note ! Type safety feature lacking in usage of Object class for making generalised classes, interface and methods.

Here comes role of Generics. Generics added type safety feature.


Simple example using Generics

General form of a generic class

class  class-name<T> {
}

Declaring a reference to generic class

class-name<type-arg-list> var name=new  class-name<type-arg-list>(constructor-arg-list);

Let's take above example with use of generics.

Example :


/* Generics example: Type of data passed as parameter 
** class one : it will allow programmer to pass a data type during creation of object.
*/

class one
{
// Create a reference variable for parameterized data type
T a;
one(T x)
{
a=x;
}
// Print value
void printValue()
{
System.out.println("Value:"+a.toString());
}
// Return object
T getObject()
{
return a;
}

public static void main(String arg[])
{
Integer b=12;
Character c='A';

// Create a object with Integer data type variable
one<Integer> ob=new one<Integer>(b);
ob.printValue();

// returing object
b=ob.getObject();

// Create a object with Character data type variable
one<Character> ob1=new one<Character>(c);
ob1.printValue();

//return object
c=ob1.getObject();
}
}
/*Output:

Value:12
Value:A
*/

Now let's understand above code.


Class one is declared as


 class one<T> {
 

Above statement will create a class with type parameter. T here is type parameter. In class if we want to declare any variable of T type then we can use T as a data type. T is a placeholder for type passed by programmer. Class one is using type parameter, that's why class one is known as generic class.

Next statement in class one is


 T a;
  

Above statement will create a reference variable for type T. For example if programmer pass a Integer type during object creation of class one then T a; will declare a Integer class reference variable.

Next we have a constructor which will take one argument of T type and assign this to class member a;


one(T x) 
{
a=x;
}

Class one printValue() method which will print value of a by using toString() method.

getObject() method is returing T type of value.


 T getObject()
{
return a;
}

Now we will see code to create a object of generic class one. We will follow general form to create a obejct of generic class.


one<Integer> ob=new one<Integer>(b);
 

Integer is specified as type parameter. T will be holding now Integer. one<Integer> ob will create a reference variable for generic class with type parameter Integer.

Next two instructions we are calling printValue() method which will print value and getObject() method which will return object of type T.


ob.printValue();

// returing object
b=ob.getObject();