Cloneable class ... is cloning an object the same as creating an object with new?

I'm not really underanding how cloneable works, I expected it to work like a copy constructor in C++ but I'm getting odd results. Im wondering if this is due to my misunderstanding of cloneable.

if I have sometthing like:

class G implements Cloneable{

private Vector a;
private Vector b;

//methods to modify vectors
...

public Object clone(){
            
      try{
            G cloned = (G)super.clone();
      
            cloned.a= (Vector)a.clone();
            cloned.b= (Vector)b.clone();
            
            return cloned;
      }catch (CloneNotSupportedException e) { return null; }
}
      
//... then I use it in another class where I generate clones and add them to another list

class UseG{

Vector lotsOfGs;


//methods...
public void somemethod(G gg){

G clonedGG = (G)gg.clone();

System.out.print(clonedGG ); //1

lotsOfGs.addElement(clonedGG); //2

}


The weird results are that the set of G's printed by 1 are totally different than the set of the those in 2
Any ideas?
polkadotAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ksivananthConnect With a Mentor Commented:
>>Note: No default functionality provided for clone

Sorry this is not correct exactly, actually if you implement cloneable interface, the default functionality is provided through reflection.
0
 
ksivananthCommented:
I am not sure of the C++ functionality but here it you responsibility to make the clone to work as you wanted. Vector has it own implementation of clone where in the cloned vector will have the same elements of source.

Note: No default functionality provided for clone.
0
 
ksivananthCommented:
below is the method description from Sun for the clone method in Object class,

"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() != xwill 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.

The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.

The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time."
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
ksivananthCommented:
0
 
polkadotAuthor Commented:
So, in my example, am I doing enough to implement a deep and accurate copy/clone of the G class?
0
 
objectsCommented:
Deep as in it copies the vector in your objects, but not a deep copuy of the Vectors. The Vector copies will contain a reference to a clone of the internal data array, not a reference to the original internal data array of each Vector.
0
 
ksivananthCommented:
ya, you have to extend Vector and override the clone method to do a deep copy of the elements of the internal array.
0
 
polkadotAuthor Commented:
Im not sure how to extend Vector and override the clone method? Can you give me an example?
0
 
objectsCommented:
why do u need to . Exactly what is it u are trying to achieve?
0
 
polkadotAuthor Commented:
I need a deep copy of the Vectors and Lists in my cloned objects.

See my example above ... with out this, //1 and //2 produce different sets.
0
 
objectsCommented:
what do you mean they produce different sets
//1 is printing out the clone, and //2 is adding it to something. What exectly is different?
0
 
polkadotAuthor Commented:
what do you mean they produce different sets
//1 is printing out the clone, and //2 is adding it to something. What exectly is different?

Programmatically, I don't know what is different, hence my question.

The clonedGGs aren't being stored in the vector the same way they are printing ...

The values in the list are different than what is printed.

I'm using many different types of vectors in my code, is it more efficient to extend the vector class and override cloneable so that the vector makes a deep copy?
0
 
objectsCommented:
wi'm confused, what are u basing this on? The code u have posted only prints one instance, what r u comparing i to?
0
 
polkadotAuthor Commented:
... oh ... after the set "lotsOfGs" has all its elements added ... I print it out and none of the elements match what the "system out print" generated.
0
 
objectsCommented:
don';t think thats anything to do with the cloning.
can u post an example that demonstates problem?
0
 
polkadotAuthor Commented:
actually, the cloneable method was the entire problem, I have it fixed now by making the class serializable and following the web site suggested:
http://www.javaworld.com/javaworld/javatips/jw-javatip76.html

Thanks
0
All Courses

From novice to tech pro — start learning today.