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).]
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.
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.