Java Generic Subclass

Introduction

A non-generic class can be the superclass of a generic subclass.


class MyClass { //from www.j a  v a  2s  . c  o  m
  int num; 
 
  MyClass(int i) { 
    num = i; 
  } 
 
  int getnum() { 
    return num; 
  } 
} 
 
// A generic subclass. 
class MySubclass<T> extends MyClass {  
  private T ob; // declare an object of type T  
    
  // Pass the constructor a reference to   
  // an object of type T.  
  public MySubclass(T o, int i) {  
    super(i); 
    ob = o;  
  }  
  
  public T getob() {  
    return ob;  
  }  
}  

public class Main {  
  public static void main(String args[]) {  
    MySubclass<String> w = new MySubclass<String>("Hello", 47); 
    
    System.out.print(w.getob() + " "); 
    System.out.println(w.getnum()); 
  }  
}



PreviousNext

Related