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