The generics means parameterized types. Parameterized types enable you 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 a single class, that automatically works 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.

Many algorithms are logically the same no matter what type of data they are being applied to. For example, the mechanism that supports a stack is the same whether that stack is storing items of type Integer, String, Object, or Thread. With generics, you can define an algorithm once, independently of any specific type of data, and then apply that algorithm to a wide variety of data types without any additional effort.


Explanation of GenClass

First, notice how GenClass is declared, T is the name of a type parameter. This name is used as a placeholder for the actual type that will be passed to GenClass when an object is created. Notice that T is contained within < >. This syntax can be generalized. Whenever a type parameter is being declared, it is specified within angle brackets. Because GenClass uses a type parameter, GenClass is a generic class, which is also called a parameterized type.

Next, T is used declare an object called ob, T is a placeholder for the actual type that will be specified when a GenClass object is created. Thus, ob will be an object of the type passed to T. For example, if type String is passed to T, then in that instance, ob will be of type String.

Next, GenClass’s constructor parameter, obj, is of type T. This means that the actual type of obj is determined by the type passed to T when GenClass object is created. Also, because both the parameter obj and member variable ob are of type T, they will both be of the same actual type when a GenClass object is created.

The type parameter T can also be used to specify the return type of a method, as is the case with the getob( ) method, because ob is also of type T, its type is compatible with the return type specified by getob( ). The showType( ) method displays the type of T by calling getName( ) on the Class object returned by the call to getClass( ) on ob. The getClass( ) method is defined by Object and is thus a member of all class types. It returns a Class object that corresponds to the type of the class of the object on which it is called. Class defines the getName( ) method, which returns a string representation of the class name.

The Javaapp class demonstrates the generic GenClass class. It first creates a version of GenClass for integers, GenClass<Integer> iob = new GenClass<Integer>(50) look closely at this declaration. First, notice that the type Integer is specified within the angle brackets after GenClass. In this case, Integer is a type argument that is passed to GenClass’s type parameter, T. This effectively creates a version of GenClass in which all references to T are translated into references to Integer. Thus, for this declaration, ob is of type Integer, and the return type getob( ) is of type Integer.

Notice that when the GenClass constructor is called, the type argument Integer is also specified. This is necessary because the type of the object (iob) to which the reference is being assigned is of type GenClass<Integer>. Thus, the reference returned by new must also be of type GenClass<Integer>. The use of antoboxing to encapsulate the value 88, which is an int, into an Integer. This works because GenClass<Integer> creates a constructor that takes an Integer argument. Because an Integer is expected, Java will automatically box 88 inside one.

The program then displays the type of ob within iob, which is Integer. Next, the program obtains the value of ob by use of int v = iob.getob( ). Because the return type of getob( ) is T, which was replaced by Integer when iob was declared, the return type of getob( ) is also Integer, which unboxes into int when assigned to getInt. Next, Javaapp declares an object of type GenClass<String>. String is substituted for T inside GenClass. This creates a String version of GenClass, as the remaining lines in the program demonstrate.

Program Source

class GenClass<T> {
    T ob;
    GenClass(T obj)
        ob = obj;
    T getOb()
        return ob;
    void showType()
        System.out.println("T Type : "+ob.getClass().getName());

public class Javaapp {
    public static void main(String[] args) {
        GenClass<Integer> iob = new GenClass<Integer>(50);
        int getInt = iob.getOb();
        System.out.println("Value  : "+getInt);
        GenClass<String> sob = new GenClass<String>("hajsoftutorial");
        String getString = sob.getOb();
        System.out.println("Value  : "+getString);

Leave a Comment