Reputation: 8939
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?
Upvotes: 3
Views: 11168
Reputation: 51030
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.
Upvotes: 7
Reputation: 11699
The reason that other implementations throw the CloneNotSupportedException is that it is part of the existing clone() method of Object. Just because a method is defined to throw the exception does not mean it has to do so. When you implement your clone() method on Shape and its derived classes, do not throw the exception.
The reason that (Shape)super.clone()
throws an exception is that this is the default behavior. Implement clone() on Shape.
Upvotes: 2
Reputation: 4397
Your class should implement Clonable interface.
Also you can use Covariant Return Types in order to return Shape/Point instead of Object. This helps you to avoid unnecessary type casts.
Upvotes: 8