M
Malcolm Ryan
Is it possible to create a type-safe Cloneable interface with generics
in Java 1.5? I want to do something like this:
interface MyCloneable<T> {
public T myClone();
}
class SomeClass implements MyCloneable<SomeClass> {
SomeClass myClone() {
SomeClass clone = new SomeClass();
// ... clone fields ...
return clone;
}
}
So that elsewhere I can do:
SomeClass thing = new SomeClass();
SomeClass clone = thing.myClone();
and know that the clone is of SomeClass, because thing is of
SomeClass. (This is something that has always annoyed me about the
standard clone method.)
When I do this, eclipse 3.1 gives me a type safety warning:
"The return type SomeClass of the method myClone() of type SomeClass
needs unchecked conversion to conform to the return type T of
inherited method."
What does this mean? It doesn't seem to affect the correctness of my
code, and I don't get this warning if I just compile with the Sun
compiler. Can I safely ignore it? Or is there a better way?
Malcolm
in Java 1.5? I want to do something like this:
interface MyCloneable<T> {
public T myClone();
}
class SomeClass implements MyCloneable<SomeClass> {
SomeClass myClone() {
SomeClass clone = new SomeClass();
// ... clone fields ...
return clone;
}
}
So that elsewhere I can do:
SomeClass thing = new SomeClass();
SomeClass clone = thing.myClone();
and know that the clone is of SomeClass, because thing is of
SomeClass. (This is something that has always annoyed me about the
standard clone method.)
When I do this, eclipse 3.1 gives me a type safety warning:
"The return type SomeClass of the method myClone() of type SomeClass
needs unchecked conversion to conform to the return type T of
inherited method."
What does this mean? It doesn't seem to affect the correctness of my
code, and I don't get this warning if I just compile with the Sun
compiler. Can I safely ignore it? Or is there a better way?
Malcolm