CSC 216/s08/feign ignorance
Cloning and the Object Superclass
A group exercise to help clarify the ambiguities related to cloning objects in Java.
The problem
Cloning objects in Java is not as straightforward as one might expect. Every class inherits a set of standard methods, including the clone() method from the Object superclass, but by default this method is protected. This prevents instances of classes outside of the object's package from cloning that object (unless it is a subclass of that object). By default, when a class implements the Cloneable interface, a call to the clone() method creates a shallow copy of that object, which basically allocates memory for a new instance and copies the memory allocated for the current object. This is ideal for ...
Participants and props
How many students will participate? What else do you need (e.g., old tennis ball, Powerpoint slides, software).
The script
Describe how to do your exercise.
References
- Learning Java - Chapter 8 : Java : Supplements
- You can insert Cloneability at any level of inheritance : Clone « Language Basics « Java