A generic class can act as a superclass or be a subclass. In a generic hierarchy, any type arguments needed by a generic superclass must be passed up the hierarchy by all subclasses.
Using a Generic Superclass
class MyClass {
T ob;
MyClass(T o) {
ob = o;
}
T getob() {
return ob;
}
}
class MySubclass extends MyClass {
V ob2;
MySubclass(T o, V o2) {
super(o);
ob2 = o2;
}
V getob2() {
return ob2;
}
}
public class Main {
public static void main(String args[]) {
MySubclass x = new MySubclass("Value is: ", 99);
System.out.print(x.getob());
System.out.println(x.getob2());
}
}
A Generic Subclass
It is perfectly acceptable for a non-generic class to be the superclass of a generic subclass.
class MyClass {
int num;
MyClass(int i) {
num = i;
}
int getnum() {
return num;
}
}
class MySubclass extends MyClass {
T ob;
MySubclass(T o, int i) {
super(i);
ob = o;
}
T getob() {
return ob;
}
}
public class Main {
public static void main(String args[]) {
MySubclass w = new MySubclass("Hello", 4);
System.out.print(w.getob() + " ");
System.out.println(w.getnum());
}
}