Pages

Generic Types JAVA


· Generic type represents classes, interfaces and methods in a type safe manner.
· Generic types can act on any type of data.
· All Generic types are subclasses of Object class, it acts on Objects only.
· Generic types act on advanced data type only.
· It is not possible to create an object to Generic type itself.
· Using generic types, we can avoid casting in many cases.

Generic Class: When we create a 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. To store different types of data into a class, we have to write the same class again and again by changing the data type of the variables. This can be avoided using a generic class.  A generic class represents a class that is type-safe. This means a generic class can act upon any data type. Generic classes and generic interfaces are also called ‘parameterized types’ because they use a parameter that determines which data type they should work upon.

Program 1: Write a program that has a class which stores any type of data.
//Example for Generic Class class MyClass<T>
{           T obj;
            MyClass (T obj)
            {
                        this.obj = obj;
            }
            T getObj ()
            {
                        return obj;
            }
} class Gen1
{           public static void main(String args[])
            {
                        Integer i1 = new Integer (10);
                        MyClass<Integer> obj1 = new MyClass<Integer>(i1);
                        System.out.println ("U stored : " + obj1.getObj() );

                        Double d1 = new Double(30.66);
                        MyClass<Double> obj2 = new MyClass<Double>(d1);
                        System.out.println ("U Stored : " + obj2.getObj() );

                        MyClass<String> obj3 = new MyClass<String>("Suresh Kumar");
                        System.out.println ("U Stored : " + obj3.getObj() );
            }
}




Generic Method: We can make a method alone as generic method by writing the generic parameter before the method return type as:
<T> returntype methodname ()
{
                                    Method code;
}
e.g.: <T> void display_data ()
        {
                        Method body;
        }

Program 2: Write a program with generic method which displays any type of data.
//Generic method example class MyClass
{           <T>void display_data (T arr[])
            {
                        for (int i=0;i<arr.length; i++)                             System.out.print ("\t" + arr[i]);
                        System.out.println ();
            }
} class Gen2
{           public static void main(String args[])
            {          MyClass obj = new MyClass ( );
                        Integer a[] = {1,2,3,4,5,6};
                        System.out.print ("Reading Integer Objects: ");
                        obj.display_data (a);

                        Float b[] = {1.1f,2.2f,3.4f};
                        System.out.print ("Reading Float Objects: ");
                        obj.display_data (b);

                        String c[] = {"Subash","Chandra","Bose"};                  System.out.print ("Reading String Objects: ");
                        obj.display_data (c);
            }
}



Generic Interface: It is possible to develop an interface using generic type concept. The general form of generic interface looks like:
interface interface_name <T>
{
             //method that accepts any object
             return_type method_name ( T object_name );
}
Here, T represents any data type which is used in the interface. We can write an implementation class for the above interface as:
                        class class_name <T> implements interface_name <T>
                        {
                                    public return_type method_name ( T object_name )
                                    {
                                                //provide body of the method
                                    }
                        }

Program 3: Write an example program for generic interface.
//A generic interface interface inter<T>
{
            void displayData (T obj);
}
class AnyClass<T> implements inter<T>
{           public void displayData (T  t1)
            {
                        System.out.println ("Entered value is : " + t1);
            }
} class Gen3
{           public static void main (String args[])
            {          AnyClass<Integer> ob1 = new AnyClass<Integer>();              ob1.displayData (new Integer (10) );
                        AnyClass<String> ob2 = new AnyClass<String>();                  ob2.displayData (new String ("Hari") );
            }