As part of our homework we are asked to implement an abstract class with a clone method. The frame for the function is given:
/**
* @effects Creates and returns a copy of this.
*/
public Object clone() {
// TODO: Implement this method
}
The Shape class has two fields:
private Point location;
private Color color;
At the instructions we’re told that the method doesn’t throw a CloneNotSupportedException exception, and are also asked why it is so. At all the examples we’ve seen on the internet the clone method does throw a CloneNotSupportedException.
Would you please point us to the reason why this clone method shouldn’t throw that exception.
The method we have written is:
/**
* @effects Creates and returns a copy of this.
*/
public Object clone() {
Shape new_shape = (Shape)super.clone();
new_shape.setColor(this.getColor());
new_shape.location = (Point)location.clone();
return new_shape;
}
It gives us an error on the (Shape)super.clone() part, saying:
Unhandled exception type CloneNotSupportedException, how should we create the clone method?
That’s because the
clone()method in theObjectis defined to throwCloneNotSupportedException:See API doc:
Object#clone()To overcome that, you either need to handle it by using a
try/catchblock or redefine it by adding athrowsclause.Update:
IMO –
Cloneableinterface, it’s telling theObjectclass that it is okay to make a clone of it. In such a case, a proper implementation of theclonemethod should call thesuper.clonemethod. Now, you can see that it’s actually theclonemethod in theObjectclass that actually makes the copy. So, we should leave up to theObject.clone()to throw thatCloneNotSupportedException. And it will do so if any class in hierarchy does not implement theCloneableinterface.I hope that makes sense.
Even though it’s kind of vast, in case you want to read about it more. It’s explained in Effective Java.