Overriding Methods in a Generic Class : Generic Class Hierarchies « Generics « Java Tutorial






class Gen<T> { 
  T ob;
  Gen(T o) { 
    ob = o; 
  } 
 
  T getObject() { 
    System.out.println("Gen's getObject(): " );
    return ob; 
  } 
} 

class Gen2<T> extends Gen<T> {

  Gen2(T o) {
    super(o);
  }
  T getObject() { 
    System.out.println("Gen2's getObject(): ");
    return ob; 
  } 
}

public class MainClass{
  public static void main(String[] arg){
    Gen<Integer> intObject = new Gen<Integer>(88); 
    Gen2<Long> longObject = new Gen2<Long>(99L);      
    
    intObject.getObject();
    longObject.getObject();
  }
  
}
Gen's getObject(): 
Gen2's getObject():








12.7.Generic Class Hierarchies
12.7.1.Generic Class Hierarchies: uses a generic superclass
12.7.2.A subclass can add its own type parameters, if needed.
12.7.3.A Generic Subclass: a nongeneric class can be the superclass of a generic subclass
12.7.4.Run-Time Type Comparisons Within a Generic Hierarchy
12.7.5.Casting within a generic class hierarchy
12.7.6.Overriding Methods in a Generic Class