Home core java Generic Classes

Generic Classes

Generic class in general – A class that can refer to any type is known as generic class.

To understand Generic classes, lets have a look on generic and nongeneric version of ArrayList Class:

Generics are introduced in java 1.5, it means until java 1.4 ArrayList class is nongeneric.

 java 1.4 non generic version of Arraylist looks like below

class ArrayList {
 
  /**
   * Takes any type of Object as input and adds to the list
   */
     add(Object o){
        //funcinality
     }
   /**
    * Takes index of an element in the list and returns as Object type
    */
     Object get(int index){
         //Functionality
      }
}

add(Object o) method can take object as the argument and hence we can add any type of object to the ArrayList class. Due to this we are not getting type safety.

- Advertisement -

The return type of get(int index) method is object hence at the time of retrieval compulsory we should perform type casting.

Since java 1.5 generic version of Arraylist looks like below

class ArrayList<T> {
 
  /**
   * Takes parameter type that defined when creating ArrayList object as input and adds to the list
   */
     add(T o){
        //funcinality
     }
   /**
    * Takes index of an element in the list and returns as parameter type that defined when creating ArrayList object
    */
     T get(int index){
         //Functionality
      }
}

based on our requirement T will be replaced with our provided parameter type. For Example
To hold only Integer type of objects we can create ArrayList object as follows.

ArrayList<Integer> l=new ArrayList<Integer>();

In this case compiler considered ArrayList class is like below.

class ArrayList<Integer> {
 
     add(Integer o){
        //funcinality
     }
   
     Integer get(int index){
         //Functionality
      }
}

Now Creating our own generic Class

class MyGenericClass<T> {
 
     T genericType;
 
    MyGenericClass(T genericType) {
        this.genericType = genericType;
    }
 
    public void show() {
        System.out.println("The type of object is :"+ genericType.getClass().getName());
    }
 
    public T getObject() {
        return genericType;
    }
 
}

Testing MyGenericClass.java class:

public class GenericClassTest {
 
    public static void main(String[] args) {
        
        MyGenericClass<String> stringType = new MyGenericClass<String>("10");
        stringType.show();
        
        MyGenericClass<Integer> integerType = new MyGenericClass<Integer>(10);
        integerType.show();
        
        MyGenericClass<Double> doubleType = new MyGenericClass<Double>(10.9);
        doubleType.show();
 
    }
 
}

Output :

The type of object is :java.lang.String
The type of object is :java.lang.Integer
The type of object is :java.lang.Double

To Understand about what is generics in java see : Java Generics Introduction

- Advertisement -
Prashanth Kumar Chilukamari
Prashanth Kumar Chilukamari
Prashanth is a post graduate in master of computer applications, and a professional software engineer with focus on Java, Spring boot, micro services and pivotal cloud. Follow him on LinkedIn or Twitter or Facebook.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles