Old style
public abstract class Generic {
public abstract void print(Object o);
class ImplInteger extends Generic {
public void print(Object o) {
/* I don't want a cast here, I want generic type usage*/
Integer n = (Integer)o;
System.out.println("The integer value is " + n);
}
}
class ImplDouble extends Generic {
public void print(Object o) {
/* I don't want a cast here, I want generic type usage*/
Double d = (Double)o;
System.out.println("The double value is " + d);
}
}
}
JDK 1.5 - solution 1
public abstract class Generic<T> {
public abstract void print(T o);
class ImplInteger<T extends Integer> extends Generic<T> {
public void print(T o) {
System.out.println("The integer value is " + o.intValue());
}
}
class ImplDouble <T extends Double> extends Generic<T> {
public void print(T o) {
System.out.println("The double value is " + o.doubleValue());
}
}
}
JDK 1.5 - solution 2
public abstract class Generic<T> {
public abstract void print(T o);
class ImplInteger extends Generic<Integer> {
public void print(Integer o) {
System.out.println("The integer value is " + o.intValue());
}
}
class ImplDouble extends Generic<Double> {
public void print(Double o) {
System.out.println("The double value is " + o.doubleValue());
}
}
}
|