Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Object class implement clone() method

In a book on Core Java, I found this excerpt :

Think about the way in which the Object class can implement clone. It knows nothing about the object at all, so it can make only a field-by-field copy. If all data fields in the object are numbers or other basic types, copying the fields is just fine. But if the object contains references to subobjects, then copying the field gives you another reference to the subobject, so the original and the cloned objects still share some information.

After reading this I was wondering, that How is the clone method originally implemented in Object Class?

What bothers me is that: how can a method in Object class make a field by field clone of a sub-class object, when it knows nothing about that class?

like image 636
VaidAbhishek Avatar asked Mar 21 '11 08:03

VaidAbhishek


People also ask

How do you implement a clone method?

Creating a copy using the clone() methodclone() to obtain the cloned object reference. The class must also implement java. lang. Cloneable interface whose object clone we want to create otherwise it will throw CloneNotSupportedException when clone method is called on that class's object.

Which method of object class is used for cloning?

For this purpose, the clone() method of an object class is used to clone an object. The Cloneable interface must be implemented by a class whose object clone to create. If we do not implement Cloneable interface, clone() method generates CloneNotSupportedException.

Why is clone method in object class in Java?

Cloning is the process of creating a copy of an Object. Java Object class comes with native clone() method that returns the copy of the existing instance. Since Object is the base class in Java, all objects by default support cloning.

What does clone () do in Java?

The class Object 's clone() method creates and returns a copy of the object, with the same class and with all the fields having the same values. However, Object. clone() throws a CloneNotSupportedException unless the object is an instance of a class that implements the marker interface Cloneable .


2 Answers

Actually, clone() is implemented in native code, so I assume it just does a memory copy (copy all the bytes) without knowing the contents.

Besides that, there is the Reflection API to gain knowlegde about a class (which would be slower, however).

like image 160
Thomas Avatar answered Oct 17 '22 18:10

Thomas


Read this from the Javadoc:

protected Object clone() -

Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:

x.clone() != x

will be true, and that the expression:

x.clone().getClass() == x.getClass()

will be true, but these are not absolute requirements. While it is typically the case that: x.clone().equals(x) will be true, this is not an absolute requirement. By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that

x.clone().getClass() == x.getClass().

By convention, the object returned by this method should be independent of this object (which is being cloned).

To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

Means when you have a subobject in your object you shouldnt just clone/copy its reference but the internal structure of this object (in order to create a new instance of it), if each object has its clean clone() methode you will be able to clone it like the parent object otherwise you will have to create a new instance of it and copy its internal premitive fields one by one.

like image 24
CloudyMarble Avatar answered Oct 17 '22 19:10

CloudyMarble