Making a clone function without throwing CloneNotSupportedException exception in Java

10,221

Solution 1

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 -

  1. You are overriding a method in a super class. Only the return type, method name, and the parameter types are considered as method signature. So, while overriding a method you can omit the throws clause in the subclass even though the method in the superclass has it.
  2. Whenever your class implements the 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.

Solution 2

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.

Solution 3

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.

Share:
10,221
SIMEL
Author by

SIMEL

Updated on June 05, 2022

Comments

  • SIMEL
    SIMEL almost 2 years

    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?