Java


Java Tutorial


Admission Enquiry Form

  

Generic Class in Java



Introduction:

When we create class with an instance variable to store an integer object, it can be used to store Integer type data only. We cannot use that instance variable to store a Float class object or a String type object.This becomes limitation.

For Example:

class Test
{
Integer x;
}




Note: Now, it is not possible to store a String or Float in this class Test, because the instance variable x is of type Integer and it can store only an Integer object.To store String type or Float type value in class Test, we can rewrite the class, as:

For String:

class Test
{
String x;
}

For Float:

class Test
{
Float x;
}

So, to store different types of data into a class, we have to write the same class again and againby changing the data type of the variables.This can be avoided if we use :

'Generic Class'





What is Generic class or interface or method?

A generic class represents a class that is type-safe. Java Generics allows us to create a single class, interface, and method that can be used with different types of data (objects).

Generic classes and interfaces are also called 'parameterized types' because they use a parameter that determines which data type they should work upon.

A generic type represents a class or an interface that is type-safe. It can act on any data type


Note: Generics does not work with primitive types (int, float, char, etc).



Code for Generic Class

// A generic class - to store any type of object
//Here, T is generic parameter which determines the datatype
class Generic1<T>
{
//declare T type object
T obj;
//constructor to initialize T type object
Generic1(T obj)
{
this.obj=obj;
}
//method returning T type object
T getobj()
{
return obj;
}
}//end of Generic1 class

public class GenericExample
{
public static void main(String[] args)
{
//creating Integer class object
Integer num=10;//This is same as: Integer num=new Integer(10);
//creating Generic1 class object and storing Integer object in it
Generic1<Integer> obj1=new Generic1<Integer> (num);
//retrieving Integer object by calling getobj()
System.out.println("Integer value is "+obj1.getobj());

        //creating Float class object
Float n=10.25f;//This is same as: Float n=new Float(10.25f);
//creating Generic1 class object and store Float object in it
Generic1<Float> obj2=new Generic1<Float> (n);
//retrieving Float object by calling getobj()
System.out.println("Float value is "+obj2.getobj());

        //we can also use Generic1 class to store String type data also
Generic1<String> obj3=new Generic1<String> ("Welcome to Compuhelp");
System.out.println("Stored string is :  "+obj3.getobj());
}//end of main method
}//end of main class


Output:

Integer value is 10
Float value is 10.25
Stored string is : Welcome to Compuhelp

Note: In the above program we created object of Generic1 class 3 times and stored 3 different objects: Integer object, Float object and String object.To create an Integer object in this program, we have written:
Integer num=10;

Observe that Integer is a wrapper class and we are storing directly 10 into num variable.
Java compiler creates Integer class object internally and stores a value 10 as shown below:
Integer num=new Integer(10);

This is called 'auto boxing'.

What is auto boxing?

Auto boxing refers to creating objects and storing primitive data types automatically by the compiler.



Code for Generic Method

// A generic method - to read and display any type of array elements
class Generic1
{
//This method accepts T type array
static <T>void display(T[] arr)
{
//use of for-each loop and read elements of an array
for(T value:arr)
{
System.out.println(value);
}
}
}//end of Generic1 class
public class GenericExample
{
public static void main(String[] args)
{
//displaying Integer type array elements using display() method
Integer arr1[]={10,20,30,40,50};
System.out.println("Values of Integer Objects : ");
Generic1.display(arr1);

        //displaying Double type array elements using display() method
Double arr2[]={10.1,20.2,30.3,40.4,50.5};
System.out.println("Values of Double Objects : ");
Generic1.display(arr2);

        //displaying String type array elements using display() method
String arr3[]={"Welcome","to","Compuhelp"};
System.out.println("Values of String Objects : ");
Generic1.display(arr3);
}//end of main method
}//end of main class


Output:

Values of Integer Objects :
10
20
30
40
50
Values of Double Objects :
10.1
20.2
30.3
40.4
50.5
Values of String Objects :
Welcome
to
Compuhelp


Code for Generic Interface

// A generic interface.
interface A<T>{
// Declare an abstract method with parameter t of type T.
public void show(T t);
}//end of interface
// This generic class implementing generic interface.
public class GenericExample<T> implements A<T>
{
public void show(T t)
{
System.out.println("Value of object is: "+t);
}
public static void main(String[] args)
{
Integer num=10;
GenericExample<Integer> t1=new GenericExample<Integer>();
t1.show(num);
Float f=23.45f;
GenericExample<Float> t2=new GenericExample<Float>();
t2.show(f);
String str="Welcome to Compuhelp";
GenericExample<String> t3=new GenericExample<String>();
t3.show(str);
}//end of main method
}//end of main class


Output:

Value of object is: 10
Value of object is: 23.45
Value of object is: Welcome to Compuhelp