Generic Type in Java

Posted By: Java Examples - 10:16 PM

Share

& Comment

The term generics here means parameterized types. Parameterized types are important because they 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, for example, 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.
It is important to understand that Java has always given you the ability to create generalized classes, interfaces, and
methods by operating through references of type Object. Because Object is the superclass of all other classes, an Object
reference can refer to any type object. Thus, in pre-generics code, generalized classes, interfaces, and methods used
Object references to operate on various types of objects


The problem was that they could not do so with type safety.
Generics add the type safety that was lacking. They also streamline the process, because it is no longer necessary to
explicitly employ casts to translate between Object and the type of data that is actually being operated upon. With
generics, all casts are automatic and implicit. Thus, generics expand your ability to reuse code and let you do so safely
and easily.



Java Version of Generic Example is given bellow :


package generic;
 *
 * @author ACHCHUTHAN
 */

/**
 * A simple generic class. Here, T is a type parameter that will be replaced by
 * a real type when an object of type Gen is created.
 */
class Gen<t> {

    T ob; // declare an object of type T

    /**
     * @param o constructor a reference t an object of type T.
     */
    Gen(T o) {
        ob = o;
    }

    /**
     * @return object
     */
    T getob() {
        return ob;
    }

    /**
     * Show type of T.
     */
    void showType() {
        System.out.println("Type of T is " + ob.getClass().getName());
    }

    public static void main(String args[]) {
        /**
         * Create a Gen reference for Integers.
         */
        Gen<integer> iOb;
        /**
         * Create a Gen<integer> object and assign its reference to iOb. Notice
         * the use of autoboxing to encapsulate the value 88 within an Integer
         * object.
         */
        iOb = new Gen<integer>(88);
        /**
         * Show the type of data used by iOb.
         */
        iOb.showType();
        /**
         * Get the value in iOb. Notice that no cast is needed.
         */
        int v = iOb.getob();
        System.out.println("value: " + v);
        System.out.println();
        /**
         * Create a Gen object for Strings.
         */
        Gen<string> strOb = new Gen<string>("Generics Test");
        /**
         * Show the type of data used by strOb.
         */
        strOb.showType();
        /**
         * Get the value of strOb. Again, notice that no cast is needed.
         */
        String str = strOb.getob();
        System.out.println("value: " + str);
    }
}

Output of this program :

run:
Type of T is java.lang.Integer
value: 88


Type of T is java.lang.String
value: Generics Test
BUILD SUCCESSFUL (total time: 1 second)

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

0 comments :

Post a Comment

Thank you for vising Java90.blogspot.com

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .