Java Generic Superclass

Introduction

The following code shows how to use a generic superclass:

// A subclass can add its own type parameters. 
class MyClass<T> {  
  T ob; // declare an object of type T  
  public MyClass(T o) {  
    ob = o;  //  w  w w  . j a v a 2s . co m
  }  
  
 
  public T getob() {  
    return ob;  
  }  
}  
// A subclass that defines a second 
// type parameter, called V. 
class MySubclass<T, V> extends MyClass<T> { 
  V ob2; 
 
  public MySubclass(T o, V o2) { 
    super(o); 
    ob2 = o2; 
  } 
 
  public V getob2() { 
    return ob2; 
  } 
} 
  
public class Main {  
  public static void main(String args[]) {  
    
    MySubclass<String, Integer> x = 
      new MySubclass<String, Integer>("Value is: ", 99);  
 
    System.out.print(x.getob()); 
    System.out.println(x.getob2()); 
  }  
}



PreviousNext

Related