Johanna
Johanna

Reputation: 27628

What is wrong with this 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;
    }
}

Upvotes: 7

Views: 28004

Answers (9)

Cojones
Cojones

Reputation: 1954

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/

Upvotes: 5

Steve Kuo
Steve Kuo

Reputation: 63054

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;
    }
}

Upvotes: 11

akarnokd
akarnokd

Reputation: 69997

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())

Upvotes: 2

Mihai Toader
Mihai Toader

Reputation: 12243

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.

Upvotes: -2

Gareth Davis
Gareth Davis

Reputation: 28059

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.

Upvotes: 4

Gábor Hargitai
Gábor Hargitai

Reputation: 564

You should simply write

return super.clone(); 

in your clone method and implement the Clonable interface.

Upvotes: 2

Itay Maman
Itay Maman

Reputation: 30723

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.

Upvotes: 6

Ganesh R.
Ganesh R.

Reputation: 4385

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.

Upvotes: 0

dreadwail
dreadwail

Reputation: 15409

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()

Upvotes: 0

Related Questions