Generic Class In Java

0

 

Generic Class In Java:-

If we want to create a class that can accept common type data as a class argument and method argument then we can create a Generic class.

It is used to provide type safety features to classes and methods both and reduce program code to declare multiple type method.

it is similar to template in c#, we can provide flexbility to class and methods to use Generic.

Example of  Generic Cass In java:-

class Ope<T>

{  T obj;

  Ope(T obj)

  {

     this.obj=obj;

  }   

    T getObject()

  {       return this.obj;

  } 

}

class OpeMain

{

   public static void main(String args[])

   {

       Ope<Integer> o = new Ope<Integer>(10);

       System.out.println(o.getObject());

       Ope<Float> o1 = new Ope<Float>(10.23F);

       System.out.println(o1.getObject());

   }

}



Tags

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)