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?
Unhandled exception type CloneNotSupportedException
That's because the clone()
method in the Object
is defined to throw CloneNotSupportedException
:
protected Object clone() throws CloneNotSupportedException
See API doc: Object#clone()
To overcome that, you either need to handle it by using a try/catch
block or redefine it by adding a throws
clause.
Update:
At the instructions we're told that the method doesn't throw a CloneNotSupportedException exception, and are also asked why it is so.
IMO -
Cloneable
interface, it's telling the Object
class that it is okay to make a clone of it. In such a case, a proper implementation of the clone
method should call the super.clone
method. Now, you can see that it's actually the clone
method in the Object
class that actually makes the copy. So, we should leave up to the Object.clone()
to throw that CloneNotSupportedException
. And it will do so if any class in hierarchy does not implement the Cloneable
interface.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.