CSC 216/s08/feign ignorance

From Expertiza_Wiki
Jump to navigation Jump to search

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 allocates memory for a new instance and copies the memory allocated for the current object. This is ideal for simple objects whose local variables only include primitive types, but if an object includes local variables that are references to other objects, then a deep copy may be preferred. Choosing between implementing a deep copy or shallow copy implementation of the clone() method depends upon the goals of the project and the overall design.

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

Formatting Resources

Formatting Help Guide from MetaWiki