M
Malcolm Ryan
I have a question about generics.
Say I have an interface:
interface Copyable<T> {
T copy();
}
and a pair of classes:
class Parent implements Copyable<Parent> {
Parent copy() {
....
}
}
class Child extends Parent {
Child copy() {
....
}
}
Because of type erasure, I can't indicate that Child implements
Copyable<Child> as well as Copyable<Parent>. Which is a problem because
I have another class which expects its type parameter to be Copyable:
and a class which expects Copyable things:
class Processor<T extends Copyable<T>> {
T process(T thing) {
T copy = thing.copy();
// .. do stuff with copy
return copy;
}
}
Now I can make a Processor<Parent> but not a Processor<Child>, which is
frustrating if I want to specify that some subclass of Processor only
works on Child. Is there any way to get around this?
Malcolm
Say I have an interface:
interface Copyable<T> {
T copy();
}
and a pair of classes:
class Parent implements Copyable<Parent> {
Parent copy() {
....
}
}
class Child extends Parent {
Child copy() {
....
}
}
Because of type erasure, I can't indicate that Child implements
Copyable<Child> as well as Copyable<Parent>. Which is a problem because
I have another class which expects its type parameter to be Copyable:
and a class which expects Copyable things:
class Processor<T extends Copyable<T>> {
T process(T thing) {
T copy = thing.copy();
// .. do stuff with copy
return copy;
}
}
Now I can make a Processor<Parent> but not a Processor<Child>, which is
frustrating if I want to specify that some subclass of Processor only
works on Child. Is there any way to get around this?
Malcolm