I have a base class, say Base
which specifies the abstract method deepCopy
, and a myriad of subclasses, say A
, B
, C
, ... Z
. How can I define deepCopy
so that its signature is public X deepCopy()
for each class X
?
Right, now, I have:
abstract class Base {
public abstract Base deepCopy();
}
Unfortunately, that means that if if I have an object from a subclass, say a
of A
, then I always have to perform an unchecked cast for a more specific deep copy:
A aCopy = (A) a.deepCopy();
Is there a way, perhaps using generics, to avoid casting? I want to guarantee that any deep copy returns an object of the same runtime class.
Edit: Let me extend my answer as covariant typing isn't enough. Say, I then wanted to implement a method like:
static <N extends Base> List<N> copyNodes(List<N> nodes) {
List<N> list = Lists.newArrayList();
for (N node : nodes) {
@SuppressWarnings("unchecked")
N copy = (N) node.deepCopy();
list.add(copy);
}
return list;
}
How could I avoid the unchecked warning?