File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Implementing Clones Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Implementing Clones" Watch "Implementing Clones" New topic
Author

Implementing Clones

Betty Reynolds
Ranch Hand

Joined: Feb 16, 2000
Posts: 111
I'm trying to understand the implementation of clones in Java. This snippet is given as an example in the tutorial with the following explanation:

The explanation is:
The implementation for Stack's clone method is relatively simple: It calls super.clone, which Stack inherits from Object and which creates and initializes an instance of the correct type. At this point, the original stack and its clone refer to the same vector. Next the method clones the vector.

Be careful: clone should never use new to create the clone and should not call constructors. Instead, the method should call super.clone, which creates an object of the correct type and allows the hierarchy of superclasses to perform the copying necessary to get a proper clone.

I don't understand how a stack is created through Stack inheriting the clone method from Object. Object's method only appears to return the string representation of the caller. (i.e., return getClass().getName() + "@" + Integer.toHexString(hashCode()
Can someone tell me what I'm missing here, Is it that the string representation given above is considered a "clone", because it is a pointer to the original one, if that's what this is.


[This message has been edited by Betty Reynolds (edited April 16, 2000).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
What makes you think that the object created is just a string representation? If you call System.out.println(clonedObject), then you will indeed see a string representation of clonedObject such as you describe - but that's because println(Object obj) is defined to call obj.toString(). The standard toString() inherited from Object generates Strings of the form ClassName@123456; many objects override this to create somethings more informative, but many do not. If you define your own Stack class without overriding toString(), you get this default behavior. But the important point is, the fact that println() shows you a String does not mean that the object is as String - that's just the way println() works.
The clone() method from Object does in fact return an object of the same class as the object from which it was called. How? Well, remember that even though the method is implemented at the Object level, it has access to the Stack instance which called it, though the "this" reference. The clone() method can use this to figure out exactly what class the object is an instance of, and create another instance of that class. This could be done using methods like getClass() from Object, getConstructor() from Class, and newInstance() from Constructor (part of java.lang.reflect). Actually these specific methods aren't used; rather Object clone() is implemented as a native method - however the point is that it is possible for the JVM to access all the info it needs to in order to create a new object of the same class as the original.

"I'm not back." - Bill Harding, Twister
Betty Reynolds
Ranch Hand

Joined: Feb 16, 2000
Posts: 111
Thanks Jim. This is embarassing. I was looking at the code for the wrong method (i.e., toString() instead of clone() in the source code). That's what comes from staying up late with little sleep (I'm cramming for the exam at the moment). I appreciate your patience.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I understand; no problem. Good luck!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementing Clones
 
Similar Threads
clonning
Question regarding Cloneable interface and Subclasses
Clone Objects
Clone an object and compare it with the oldest one
How to Clone an Object??