Java hierarchy generic class : Generic Class « Generics « Java






Java hierarchy generic class

Java hierarchy generic class
   
/*
Java 2, v5.0 (Tiger) New Features
by Herbert Schildt
ISBN: 0072258543
Publisher: McGraw-Hill/Osborne, 2004
*/
// Here, T is bound by Object by default. 
class Gen<T> {  
  T ob; // here, T will be replaced by Object 
    
  Gen(T o) {  
    ob = o;  
  }  
  
  // Return ob.  
  T getob() {  
    return ob;  
  }  
}  
 
// Here, T is bound by String. 
class GenStr<T extends String> { 
  T str; // here, T will be replaced by String 
 
  GenStr(T o) {  
    str = o;  
  }  
 
  T getstr() { return str; } 
}

public class GenTypeDemo {  
  public static void main(String args[]) {  
    Gen<Integer> iOb = new Gen<Integer>(99);  
    Gen<Float> fOb = new Gen<Float>(102.2F); 
 
    System.out.println(iOb.getClass().getName()); 
    System.out.println(fOb.getClass().getName()); 
  } 
}

           
         
    
    
  








Related examples in the same category

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