Object Integrity & Security: Duplicating Objects: Part 2

This series, The Object-Oriented Thought Process, is intended for someone just learning an object-oriented language and who wants to understand the basic concepts before jumping into the code, or someone who wants to understand the infrastructure behind an object-oriented language he or she is already using. These concepts are part of the foundation that any programmer will need to make the paradigm shift from procedural programming to object-oriented programming.

In keeping with the code examples used in the previous articles, Java will be the language used to implement the concepts in code. One of the reasons that I like to use Java is because you can download the Java compiler for personal use at the Sun Microsystems Web site http://java.sun.com/. You can download the standard edition, J2SE 5.0, at http://java.sun.com/j2se/1.5.0/download.jsp to compile and execute these applications. I often reference the Java J2SE 5.0 API documentation and I recommend that you explore the Java API further. Code listings are provided for all examples in this article as well as figures and output (when appropriate). See the first article in this series for detailed descriptions for compiling and running all the code examples.

The code examples in this series are meant to be a hands-on experience. There are many code listings and figures of the output produced from these code examples. Please boot up your computer and run these exercises as you read through the text.

For the past several months, my articles have been exploring various issues regarding object integrity, security, and performance. In this month's article, you continue with this theme and begin a discussion on how objects are duplicated. This is not a trivial issue. Whereas copying primitives such as numbers is mostly straightforward, copying objects is a more complicated task. Completely duplicating an object is a multi-step process.

Cloning Objects

Please review the final step in the process in cloning objects that you covered in the previous article.

The object class in Java provides a method called clone() that performs the physical copy of an object. Taking a look at the Java documentation, you see the definition for clone() (remember that all Java objects inherit from the object class).

In Listing 1, pay special attention to the fact that the Dog class implements the Clonable interface, and that you must make provisions to catch the CloneNotSupportedException exception. For a more detailed description of this listing, you can reference last month's article: http://www.developer.com/security/article.php/3675326.

After compiling and running this application, you can run the code that produces the output in Figure 1.

Figure 1: Cloning an Object

The important concept here is that there are now two totally separate objects. You can represent this graphically to illustrate how the objects are represented in memory. Diagram 1 shows that in this case you not only have two references, you also have two separate objects. This was not the case when you simply created a reference and pointed the reference to the original object memory.

Diagram 1: An Object with two References and two Objects (same content)