A simple generic class. : Generic Class « Generics « Java






A simple generic class.

A simple generic class.
   
class Gen<T> {
  T ob; // declare an object of type T

  Gen(T o) {
    ob = o;
  }

  T getob() {
    return ob;
  }

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

public class GenDemo {
  public static void main(String args[]) {
    Gen<Integer> iOb;
    iOb = new Gen<Integer>(88);

    iOb.showType();

    int v = iOb.getob();
    System.out.println("value: " + v);

    System.out.println();

    Gen<String> strOb = new Gen<String>("Generics Test");

    strOb.showType();

    String str = strOb.getob();
    System.out.println("value: " + str);
  }
}


           
         
    
    
  








Related examples in the same category

1.Demonstrate the non generic classDemonstrate the non generic class
2.Stats attempts (unsuccessfully) to create a generic class
3.A simple generic class hierarchy.A simple generic class hierarchy.
4.A nongeneric class can be the superclass of a generic subclass.A nongeneric class can be the superclass of a generic subclass.
5.Use the instanceof operator with a generic class hierarchy. Use the instanceof operator with a generic class hierarchy.
6.Java hierarchy generic classJava hierarchy generic class
7.Custom Generic Object TesterCustom Generic Object Tester
8.Pair of template arguments
9.Generic pair structure
10.Simple STL Pair Implementation