Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with this clone()?

Tags:

java

clone

I have written this clone method for when the parent of the Employee class is abstract and the clone() method in the parent class is abstract.I wanted to copy the primitive data type of the Employee's object with this code instead of copying each primitive data type individually, but this code has problem with the line that I call clone() method. (This code is in Employee class)

public Object clone() {
    Object obj = new Object();
    Object object = obj.clone();  //Emphasis here
    return object;

}

the error is: The method clone() from the type Object is not visible.

But my Employee class is in the class hierarchy which can access the protected clone() method in the Object class.

This is my simple Employee class:

public class Employee extends Person implements Cloneable {
private int ID;

public Employee() {
    ID = 0;
}

public void setID(int ID) {
    this.ID = ID;
}

public int getID() {
    return ID;
}

public Object clone1() throws CloneNotSupportedException {
    try {
        Object obj = new Object();

        Object object = obj.clone();
        return object;
    } catch (CloneNotSupportedException ex) {
        return null;
    }
}
like image 510
Johanna Avatar asked Jun 27 '09 07:06

Johanna


2 Answers

The standard pattern for making a class cloneable is:

  1. Implement Cloneable
  2. Override the clone() method and make it public
  3. In clone() call super.clone() and then copy any mutable object's state

You should not create a new object using new. The proper way is to call super.clone() for a new instance. Object's clone() is special and will create a new copy of the object and copy its primitive fields and references.

For example:

public class Person implements Cloneable {
    protected String name;
    // Note that overridden clone is public
    public Object clone() {
        Person clone = (Person)super.clone();
        // No need to copy name as the reference will be
        // copied by Object's clone and String is immutable
        return clone;
    }
}

public class Employee extends Person {
    protected int id;
    protected java.awt.Point location;
    public Object clone() {
        Employee  clone = (Employee )super.clone();
        // No need to copy id as Object's clone has already copied it
        // Need to clone location as Point is mutable and could change
        clone.location = location.clone();
        return clone;
    }
}
like image 103
Steve Kuo Avatar answered Oct 01 '22 19:10

Steve Kuo


Java's cloning mechanism is somewhat awkward. In order to be able to clone itself the class must do two things. First it must implement Clonable. Second it must override clone() and make it public.

In your example, you overrode clone() but you are invoking clone() not on the Employee class but rather on the Object.class() where clone() is only protected.

like image 33
Itay Maman Avatar answered Oct 01 '22 19:10

Itay Maman