CSC 216/s08/feign ignorance: Difference between revisions
mNo edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
== | ==Cloning and the Object Superclass== | ||
A group exercise to help clarify the ambiguities related to cloning objects in Java. | |||
===The problem=== | ===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=== | ===Participants and props=== | ||
Line 23: | Line 20: | ||
*[http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Supplements/Chapter08/cloning.html Learning Java - Chapter 8 : Java : Supplements] | *[http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Supplements/Chapter08/cloning.html Learning Java - Chapter 8 : Java : Supplements] | ||
*[http://www.java2s.com/Code/Java/Language-Basics/YoucaninsertCloneabilityatanylevelofinheritance.htm You can insert Cloneability at any level of inheritance : Clone « Language Basics « Java] | *[http://www.java2s.com/Code/Java/Language-Basics/YoucaninsertCloneabilityatanylevelofinheritance.htm You can insert Cloneability at any level of inheritance : Clone « Language Basics « Java] | ||
===Formatting Resources=== | |||
[http://meta.wikimedia.org/wiki/Help:Wikitext_examples Formatting Help Guide from MetaWiki] |
Revision as of 07:23, 27 March 2008
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