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> { T ob;/*from w w w . j a v a 2s . c o m*/ MyClass(T o) { ob = o; } T getob() { return ob; } } class MySubclass<T, V> extends MyClass<T> { 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<String, Integer> x = new MySubclass<String, Integer>("Value is: ", 99); System.out.print(x.getob()); System.out.println(x.getob2()); } }
The code above generates the following result.
It is perfectly acceptable for a non-generic class to be the superclass of a generic subclass.
class MyClass {//from ww w . j ava 2 s . c o m int num; MyClass(int i) { num = i; } int getnum() { return num; } } class MySubclass<T> 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<String> w = new MySubclass<String>("Hello", 4); System.out.print(w.getob() + " "); System.out.println(w.getnum()); } }
The code above generates the following result.
The instanceof operator can be applied to objects of generic classes.
class Gen<T> { T ob;//ww w . ja v a 2s . co m Gen(T o) { ob = o; } T getob() { return ob; } } class Gen2<T> extends Gen<T> { Gen2(T o) { super(o); } } public class Main { public static void main(String args[]) { Gen<Integer> iOb = new Gen<Integer>(88); Gen2<Integer> iOb2 = new Gen2<Integer>(99); Gen2<String> strOb2 = new Gen2<String>("Generics Test"); System.out.println("iOb2 is instance of Gen2"+(iOb2 instanceof Gen2<?>)); System.out.println("iOb2 is instance of Gen"+(iOb2 instanceof Gen<?>)); System.out.println("strOb2 is instance of Gen2"+(strOb2 instanceof Gen2<?>)); System.out.println("strOb2 is instance of Gen"+(strOb2 instanceof Gen<?>)); System.out.println("iOb is instance of Gen2"+(iOb instanceof Gen2<?>)); System.out.println("iOb is instance of Gen"+(iOb instanceof Gen<?>)); } }
The output:
A method in a generic class can be overridden like any other method.
class Gen<T> { T obj;/*from www .j a v a 2 s .com*/ Gen(T o) { obj = o; } T getob() { System.out.print("Gen's getob(): "); return obj; } } class Gen2<T> extends Gen<T> { Gen2(T o) { super(o); } T getob() { System.out.print("Gen2's getob(): "); return obj; } } public class Main { public static void main(String args[]) { Gen<Integer> iOb = new Gen<Integer>(88); Gen2<String> strOb2 = new Gen2<String>("Generics Test"); System.out.println(iOb.getob()); System.out.println(strOb2.getob()); } }
The code above generates the following result.
You can cast one instance of a generic class into another only if the two are compatible and their type arguments are the same.
For example, assuming the following program, this cast is legal:
class Gen<T> { T ob;/*from w ww . j a va 2 s . c om*/ Gen(T o) { ob = o; } T getob() { return ob; } } class Gen2<T> extends Gen<T> { Gen2(T o) { super(o); } } public class Main { public static void main(String args[]) { Gen<Integer> iOb = new Gen<Integer>(88); Gen2<Integer> iOb2 = new Gen2<Integer>(99); Gen2<String> strOb2 = new Gen2<String>("Generics Test"); iOb = (Gen<Integer>) iOb2; } }
because iOb2 is an instance of Gen<Integer>. But, this cast:
class Gen<T> { T ob; Gen(T o) { ob = o; } T getob() { return ob; } } class Gen2<T> extends Gen<T> { Gen2(T o) { super(o); } } public class Main { public static void main(String args[]) { Gen<Integer> iOb = new Gen<Integer>(88); Gen2<Integer> iOb2 = new Gen2<Integer>(99); Gen2<String> strOb2 = new Gen2<String>("Generics Test"); //iOb = (Gen<Long>) iOb2;//wrong } }
is not legal because iOb2 is not an instance of Gen<Long>.