Solved

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

Posted on 2006-06-22
17
313 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:polkadot
  • 6
  • 6
  • 5
17 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 16965281
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 16965288
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
 
LVL 26

Accepted Solution

by:
ksivananth earned 100 total points
ID: 16965313
>>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
 
LVL 26

Expert Comment

by:ksivananth
ID: 16965320
0
 

Author Comment

by:polkadot
ID: 16966009
So, in my example, am I doing enough to implement a deep and accurate copy/clone of the G class?
0
 
LVL 92

Expert Comment

by:objects
ID: 16966428
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 16971578
ya, you have to extend Vector and override the clone method to do a deep copy of the elements of the internal array.
0
 

Author Comment

by:polkadot
ID: 16973966
Im not sure how to extend Vector and override the clone method? Can you give me an example?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 92

Expert Comment

by:objects
ID: 16974023
why do u need to . Exactly what is it u are trying to achieve?
0
 

Author Comment

by:polkadot
ID: 16974045
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
 
LVL 92

Expert Comment

by:objects
ID: 16974056
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
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 16974058
0
 

Author Comment

by:polkadot
ID: 16975072
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
 
LVL 92

Expert Comment

by:objects
ID: 16975115
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
 

Author Comment

by:polkadot
ID: 16975524
... 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
 
LVL 92

Expert Comment

by:objects
ID: 16977026
don';t think thats anything to do with the cloning.
can u post an example that demonstates problem?
0
 

Author Comment

by:polkadot
ID: 16977424
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now