Overriding Methods in a Generic Class
A method in a generic class can be overridden like any other method.
class Gen<T> {
T obj;
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());
}
}
Home
Java Book
Language Basics
Java Book
Language Basics
Generics:
- Generic Class
- Generic Bounded Types
- Generic Wildcard Arguments
- Generic Bounded Wildcards
- Generic Method
- Generic Constructors
- Generic Interfaces
- Raw Types and Legacy Code
- Generic Class Hierarchies
- Run-Time Type Comparisons Within a Generic Hierarchy
- Overriding Methods in a Generic Class
- Generic Restrictions
- Generic Array Restrictions