Generics

What is Generics?
Generics means parameterized types.
Parameterized types enable users to create classes, interfaces, and methods in which the type of data upon which they operate is specified as a parameter.
Using generics, it is possible to create classes that automatically work with different types of data.
A class, interface, or method that operates on a parameterized type is called generic, as in generic class or generic method
 

Need for generics:
  • Through the use of generics, it is possible to create classes, interfaces, and methods that will work in a type-safe manner with various kinds of data. With generics, an algorithm can be created once, independent of any specific type of data, and then applied to a wide variety of data types without any additional effort.
    • Example – Stack algorithm once created can work with any types of data – String, Integer, Object, etc.
  • In addition to being a powerful language element on its own, generics also enables existing feature to be substantially improved.
    • Example – A collection is a group of objects. The Collections Framework defines several classes that manage collections – example lists and maps. The collection classes have always been able to work with any type of object. The benefit of using generics is that the collection classes can now be used with complete type safety.

The General Form of a Generic Class

 

Syntax for declaring a generic class:
class class-name <type-param-list>
{
// …
}

Syntax for declaring a reference to a generic class:
class-name<type-arg-list> var-name;

Syntax for instance creation to a generic class:
var-name = new class-name<type-arg-list>(cons-arg-list);

Generic Class with Multiple Type Parameters
To specify two or more type parameters in a generic type use a comma-separated list.

Creating a Generic Method
  1. Methods inside a generic class can make use of a class type parameter and are, therefore, automatically generic relative to the type parameter.
  2. Generic methods may use one or more type parameters of its own.
  3. Can also create generic method within a non-generic class.

Syntax for a generic method:
 <type-param-list> return-type method-name(param-list)
{
//..
}

// Generic programming – Example
// T1 – Type Parameter
// T1 – will be replaced by real type when object is created
class Gen<T1>
{
     T1 x;
     Gen(T1 v)
     {
          x = v;
     }
     public void fnDisplay()
     {
          System.out.println(“x = “+x);
          System.out.println(“Data type = “+x.getClass().getName());
     }
}
class GenPgm1
{
     public static void main(String as[])
     {
          Gen<Integer> g1 = new Gen<Integer>(5);
          g1.fnDisplay();
          Gen<Double> g2 = new Gen<Double>(4.5);
          g2.fnDisplay();
          Gen<String> g3 = new Gen<String>(“asd”);
          g3.fnDisplay();
     }
}

// Generic programming
// Two data types
class Gen<T1, T2>
{
     T1 x;
     T2 y;
     Gen(T1 v1, T2 v2)
     {
          x = v1;
          y = v2;
     }
     public void fnDisplay()
     {
          System.out.println(“x = “+x);
          System.out.println(“Data type of x = “+x.getClass().getName());
          System.out.println(“y = “+y);
          System.out.println(“Data type of y = “+y.getClass().getName());
     }
}
class GenPgm2
{
     public static void main(String as[])
     {
          Gen<Integer, String> g1 = new Gen<Integer, String>(5, “asd”);
          g1.fnDisplay();
          Gen<Double, String> g2 = new Gen<Double, String>(4.5, “sdf”);
          g2.fnDisplay();
     }
}

// Generic programming and Arrays
class Gen<T1>
{
     T1 x[];
     Gen(T1 []v1)
     {
          x = v1;
     }
     public void fnDisplay()
     {
          System.out.println(“Array elements”);
          for(int i = 0;i<x.length;i++)
              System.out.println(x[i]);
     }
}
class GenPgm3
{
     public static void main(String as[])
     {
          Integer [] ia = {2,4,6,8};
          Gen<Integer> g1 = new Gen<Integer>(ia);
          g1.fnDisplay();
          String []sa = {“cse”, “it”, “eee”, “ece”};
          Gen<String> g2 = new Gen<String>(sa);
          g2.fnDisplay();
     }
}