7

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;
    }
}
1
  • +1. Because as I understand it know the situation is actually a bit tricky, not just a simple clone() case.
    – akarnokd
    Jun 27, 2009 at 9:10

9 Answers 9

11

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;
    }
}
2
  • True. Wonder how do you express the need for the children of Person to be Cloneable - or in general: copy themselves on demand? Is it enough to have a Cloneable superclass?
    – akarnokd
    Jun 27, 2009 at 17:34
  • 1
    All subclasses inherit the parent's interfaces. So Person is Cloneable .
    – Steve Kuo
    Jun 28, 2009 at 5:32
6

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.

4
  • I didn't get your answer.what do you mean with the line 3 and 4 of your answer??
    – Johanna
    Jun 27, 2009 at 8:12
  • This guy is right, Object doesn't have a clone method, only ICloneable stuff does. I'd say you need to cast obj to ICloneable then call clone BUT you are building obj with new Object, so that will really not have clone(). What exactly do you think your code should do?
    – Blindy
    Jun 27, 2009 at 8:18
  • I think IClonable is C#? But still good point on cloning Object instead of itself.
    – akarnokd
    Jun 27, 2009 at 8:22
  • my code should refer to the clone() method in the Object class for copying all the primitive data types instead of copying them one by one with myself.
    – Johanna
    Jun 27, 2009 at 8:23
5

I think the current green answer is bad , why you might ask?

  • It adds a lot of code
  • It requires you to list all fields to be copied and do this
  • This will not work for Lists when using clone() (This is what clone() for HashMap says: Returns a shallow copy of this HashMap instance: the keys and valuesthemselves are not cloned.) so you end up doing it manually (this makes me cry)

Oh and by the way serialization is also bad, you might have to add Serializable all over the place (this also makes me cry).

So what is the solution:

Java Deep-Cloning library The cloning library is a small, open source (apache licence) java library which deep-clones objects. The objects don't have to implement the Cloneable interface. Effectivelly, this library can clone ANY java objects. It can be used i.e. in cache implementations if you don't want the cached object to be modified or whenever you want to create a deep copy of objects.

Cloner cloner=new Cloner();
XX clone = cloner.deepClone(someObjectOfTypeXX);

Check it out at http://code.google.com/p/cloning/

4

Mister Bloch of Effective Java has some interesting words to say on the use of clone.

http://www.artima.com/intv/bloch13.html

Generally the current thinking is to avoid the use of clone as it is error prone and greatly misunderstood, implementing copy constructor is an alternative strategy or using a brute force approach of deep copying the object using serialisation.

1
  • +1 providing a readable article instead of just a book reference.
    – akarnokd
    Jun 27, 2009 at 8:32
2

Did you implement the Cloneable interface on your object?

However, there are very few cases I would use clone for copying an object. One such safe example is array.clone(). I would rather use the copy-constructor idiom or manually copying / assigning values around.

There is Item#11 in Effective Java (2nd edition) about the background issue. Cloneable interface is a special kind of interface as it modifies the Object class' behavior regarding the cloning. Basically it is a feature enabling classinterface in Java.

Edit: Based on your example you might need to wrap the clone() call in a try-catch of CloneNotSupportedException in a general case.

Edit2: Rephrased my answer

Edit3: Did you override the clone() in the public context? In the sample you gave you try to clone an Object, which is in the java.lang package - hardly the package your code is in.

Edit4: I think the answer is already in the other posts, just wanted to reflect on the underlying issue.

Edit5: Try this:

public Object clone1() throws CloneNotSupportedException {        
    return super.clone();        
}

Edit6 Then name your method public abstract Object copy() for example and in the implementation, use the super.clone() - to avoid confusion.

Edit7 I did some eclipsing and came out with the following solution:

public class Cloner {
    public static abstract class Person {
       protected abstract Object clone1() throws CloneNotSupportedException;
       public Object copy() throws CloneNotSupportedException {
           return clone1();
       }
    }
    public static class Employee extends Person implements Cloneable {
        @Override
        protected Object clone1() throws CloneNotSupportedException {
            return super.clone();
        }
    
    }
    public static void main(String[] args) throws Exception {
        new Employee().copy();
    }
}

But basically it is the same concept as renaming your abstract method to something else than clone().

Edit8: Fixed my sample, now it works without exception.

(But the actual credit goes to Gábor Hargitai for super.clone())

10
  • Yes I have implemented Cloneable interface.
    – Johanna
    Jun 27, 2009 at 8:01
  • I have put it in try catch block but it still has that error.
    – Johanna
    Jun 27, 2009 at 8:08
  • I can not use super.clone() . here super is Person and Person has an abstract clone()!!! How can I call super.clone()????
    – Johanna
    Jun 27, 2009 at 8:35
  • in such a way that you recommended,will show an error:can not directly invoke the abstract method clone() for the type Person
    – Johanna
    Jun 27, 2009 at 8:42
  • 1
    Okay, I see it now. You ran into an 'impossible' situation of declaring an abstract method which exists in the class hierarchy above the Person class. If you can, use a different name than clone(). edit
    – akarnokd
    Jun 27, 2009 at 9:18
2

You should simply write

return super.clone(); 

in your clone method and implement the Clonable interface.

7
  • I can not because the super class is for example Person which has the abstract clone() method.
    – Johanna
    Jun 27, 2009 at 8:14
  • That's not a problem, because the actual cloning is performed by the Object class - you only need to implement Cloneable, unless you created an abstract clone() method on the Person class.
    – akarnokd
    Jun 27, 2009 at 8:17
  • 1
    Why does it have an abstract clone method? You can make clone() non-abstract in Person and do the same. Jun 27, 2009 at 8:18
  • Or just remove clone() from Preson entirely.
    – akarnokd
    Jun 27, 2009 at 8:24
  • I want to make it in such a way for knowing that Can I wrote this code.
    – Johanna
    Jun 27, 2009 at 8:25
0

Have you implemented the appropriate interface? It may be as simple as that, though you may have already accounted for this.

See here for more information: http://java.sun.com/javase/6/docs/api/java/lang/Object.html#clone()

1
  • @dxmio:Yes I have implemented the Cloneable interface.but also it has this error,and I don't know why??:((
    – Johanna
    Jun 27, 2009 at 8:00
0

I am not very familiar with Java but this may help: http://en.wikipedia.org/wiki/Clone_(Java_method)

An Excerpt from the post:

Another disadvantage is that one often cannot access the clone() method on an abstract type. Most interfaces and abstract classes in Java do not specify a public clone() method. As a result, often the only way to use the clone() method is if you know the actual class of an object; which is contrary to the abstraction principle of using the most generic type possible. For example, if one has a List reference in Java, one cannot invoke clone() on that reference because List specifies no public clone() method. Actual implementations of List like ArrayList and LinkedList all generally have clone() methods themselves, but it is inconvenient and bad abstraction to carry around the actual class type of an object.

-2

Basically in order to have a properly cloneable object is enough to have a public clone() method implemented in that class.

The Cloneable interface is a marker interface used to signal to the VM that it's safe to implement the default protected clone() method as a field by field copy.

In order to properly implement the clone method for a class you should do declare a public method clone like this().

public Object clone() {
   return super.clone();
}

A good working implementationk will create a new object and properly assign the fields as the business logic reaquires:

public Object clone() {
   CurrentClass newObject = new CurrentClass();

   newObject.field1 = this.field1; // for simple types: int, long, etc
   newObject.referenceField = this.referenceField.clone(); // for agregate objects or references.
   return newObject;
}

The conclusion: declare a public clone method. If you want to have the default implementation as a field by field copy call super and mark the class as Cloneable If you want only custom cloning you can ignore the Cloneable mark.

3
  • good,but I clone the primitive type one by one,I don't want this.I want to clone those primitive data types at once without using "newObject.field1 = this.field1;"
    – Johanna
    Jun 27, 2009 at 9:00
  • this is how the clone mechanism works :-). If you want something else you need to implement it yourself (with code generation ala asm and/or reflection). Jun 27, 2009 at 15:27
  • 1
    It's considered bad practice to new an object in clone. You should use super.clone() to get the new cloned instance.
    – Steve Kuo
    Jun 27, 2009 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.