Java cloning abstract objects
I'm wondering if there is any way to do the following. I have an abstract
class, Shape, and all its different subclasses and I want to override the
clone method. All I want to do in the method is create a new Shape from
the toString() of the current one. Obviously I can't do the following
because Shape is abstract. Is there another way to do this because
overriding clone in every subclass just for a simple name change seems
useless.
public abstract class Shape {
public Shape(String str) {
// Create object from string representation
}
public Shape clone() {
// Need new way to do this
return new Shape(this.toString());
}
public String toString() {
// Correctly overriden toString()
}
}
No comments:
Post a Comment