generics2 (1)
generics2 (1)
If you have worked with C++ before, then Java Generics is the same as templates in C+
+. Java Generics allow you to include a parameter in your class/method definition which
will have the value of a primitive data type.
Similarly, you can write a generic method with a parameterized type for sorting an array
and then instantiate this method to any primitive type.
Java Generics are mostly used with the collections framework of Java. The different
collections like LinkedList, List, Map, HashMap, etc. use Generics for implementation.
Generics provide type-safety as the type checking is done at compile time thus making
your code more stable.
Let us now more into the details of Generic classes and methods as well as other related
topics.
Generic Classes
A Generic class is the same as a normal class except that the classname is followed by
a type in angular brackets.
class class_name<T>
{
class variables;
…..
class methods;
}
Once the class is defined, you can create objects of any data type that you want
as follows:
class_name <T> obj = new class_name <T> ();
For Example, for Integer object the declaration will be:
class_name <Integer> obj = new class_name<Integer>;
Similarly, for the String data type, the object will be:
class_name <String> str_Obj = new class_name<String>;
An example implementation for the Generic class is shown below.
class MyGenericClass<T>
{
T obj;
void add(T obj)
{
this.obj=obj;
}
T get()
{
return obj;
}
}
class Main
{
public static void main(String args[])
{
MyGenericClass<Integer> m_int=new MyGenericClass<Integer>();
m_int.add(2);
MyGenericClass<String>mstr=new MyGenericClass<String>();
mstr.add("SoftwaretestingHelp");
In the above program, a class MyGenericClass is a generic class. It has two methods
i.e. add and get. The method add initializes the generic object while the get methods
return the object.
In the main function, we declare two objects of Integer and String type each. We initialize
both these objects with their respective initial values using the add method and then
output the contents of these objects using the get method.
We presented the Generic class example above with one type parameter. But in reality,
a class can have more than one type parameter as well. In this case, the type
parameters are separated by a comma.
class Main
{
public static void main (String[] args)
{
Test_Generics<String, Integer>obj =
newTest_Generics<String, Integer>("Java Generics", 1);
obj.print();
}
}
Output:
In this program, we have two type parameters i.e. T1 and T2. We have functions to
initialize the member objects and also to print the contents. In the main function, we
declare an object with two types i.e. String and Integer. The output of the program shows
the contents of the created object.
Just like classes, you can also have Generic interfaces. We will learn all about interfaces
in a separate topic.
So in general, bounded type parameters are mostly used when you want to restrict the
data types to be used in your generics code.