tags:

views:

1558

answers:

10

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

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

byte
@dxmio:Yes I have implemented the Cloneable interface.but also it has this error,and I don't know why??:((
Johanna
+2  A: 

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

kd304
Yes I have implemented Cloneable interface.
Johanna
I have put it in try catch block but it still has that error.
Johanna
I can not use super.clone() .here super is Person and Person has an abstract clone()!!!How can I call super.clone()????
Johanna
in such a way that you recommended,will show an error:can not directly invoke the abstract method clone() for the type Person
Johanna
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]
kd304
excellent,I have nothing to say ,just thank you for all :">
Johanna
I have copied this code in Eclipse and it has no error(good)but would you mind explaining more about super.clone() in Employee class?? super here means Person but Person class has an abstract clone() method that has no body,So with calling this kind of method the output will be the one that we want????
Johanna
No, the point is the Person has clone1() instead of clone()! This way you don't get the overrid paradox. Clients to your class need call the copy() method to get a clone - and Person expresses its need to its children to provide a common means for obtaining a copy of the class. [edit]
kd304
oh ,NO now I get what you mean,yes you are right but I have edited my question (I mean that the clone()method is abstract)
Johanna
Now I don't understand you. Calling protected method on an Object instance/reference will never compile (unless you are a JDK developer). If you tried you couldn't call finalize() on the Object because of the same reason. However, If you have a class which explicitely exposes these protected methods via public, calling these methods on a reference of this class is allowed. I have a feeling you misunderstood your original assignment requirement to use clone() for copying.
kd304
-1 spurious downvote.
kd304
A: 

Hi, 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.

Ganesh R.
+3  A: 

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.

Itay
I didn't get your answer.what do you mean with the line 3 and 4 of your answer??
Johanna
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
I think IClonable is C#? But still good point on cloning Object instead of itself.
kd304
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
A: 

You should simply write

return super.clone();

in your clone method and implement the Clonable interface.

Gábor Hargitai
+1, based on Johanna's comment to Italy
kd304
I can not because the super class is for example Person which has the abstract clone() method.
Johanna
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.
kd304
Why does it have an abstract clone method? You can make clone() non-abstract in Person and do the same.
Gábor Hargitai
Or just remove clone() from Preson entirely.
kd304
I want to make it in such a way for knowing that Can I wrote this code.
Johanna
@Johanna: Sorry, I can't understand your last sentence. Could you rephrase it please?
kd304
@kd304:sure,I have to write a code which the parent class is abstract and has an abstract clone() method and you want to call the clone method in its child class,and I tried in such a way (return super.clone();)but it will show the error:can not directly invoke the abstract method clone() for the type Person.
Johanna
+1  A: 

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.

Gareth Davis
+1 providing a readable article instead of just a book reference.
kd304
A: 

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.

Toader Mihai Claudiu
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
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).
Toader Mihai Claudiu
It's considered bad practice to new an object in clone. You should use super.clone() to get the new cloned instance.
Steve Kuo
+2  A: 

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;
    }
}
Steve Kuo
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?
kd304
All subclasses inherit the parent's interfaces. So Person is Cloneable .
Steve Kuo
+2  A: 

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();
cloner.deepClone(someObject);

Check it out at http://robust-it.co.uk/clone/index.php

A: 

In addition the "Java Deep Cloning Library" there is also the utility at http://www.genericdeepcopy.com/ which aims to deep clone any Java object..

Aaron