May 29, 2020
Hot Topics:

Object Integrity & Security: Duplicating Objects, Part 3

  • By Matt Weisfeld
  • Send Email »
  • More Articles »

This is the thirty-sixth installment in a series of articles about fundamental object-oriented (O-O) concepts and related object-oriented technologies. The material presented in these articles is based on material from the second edition of my book, The Object-Oriented Thought Process, 2nd edition. The Object-Oriented Thought Process is intended for anyone who needs to understand basic object-oriented concepts and technologies before jumping directly into the code. Click here to start at the beginning of this entire series.

If you would like to start at part 1 of this topic, Object Integrity & Security: Duplicating Objects, click here.

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 (http://www.developer.com/design/article.php/3304881).

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, you have been exploring various issues regarding object integrity, security, and performance. In this month's article, you continue with this theme and begin to learn how objects are duplicated. This is not a trivial issue. Although copying primitives such as numbers is mostly straightforward, copying objects is a more complicated task. Completely duplicating an object is a multi-step process.

It is important to realize that duplicating objects can be a complicated process, and that not fully understanding the process can lead to problems. These problems are akin to the difference between a syntax error and a logic error. You can have code that is compiling and executing, yet the results can be totally incorrect and possibly dangerous.

Copying Objects

In the two previous articles, you covered the topic of cloning objects. One of the primary points discussed was the fact that duplicating objects is not exactly straightforward.

The phrase 'not exactly straightforward' might not be strong enough. The term 'confusing' might be more appropriate. As you discussed, the problem concerns the references and how they are handled.

Continue with the example you used previously. There were two classes, Duplicate.java and Dog.java, as seen in Listing 1 and Listing 2.

Listing 1: The Previous Example (Dog.java)

// Class Dog
class Dog implements Cloneable {

   String name;
   String breed;

   Dog ref;

   public Dog(String n, String b) {

      name = n;
      breed = b;


   public Dog copy(String n, String b) {

      ref = (Dog)clone();


      return ref;


   public Object clone() {
     try {
         System.out.println("Inside clone");
         return super.clone();
      } catch (CloneNotSupportedException e) {
         throw new InternalError(e.toString());

   public String getName() {

      return name;

   public void setName(String n) {

      name = n;


   public String getBreed() {

      return breed;

   public void setBreed(String b) {

      breed = b;



These examples are purposely kept as small as possible. Thus, you have only two attributes for the Dog class and their corresponding access modifiers.

In the Duplicate class, you create a single new object called fido and then copy the fido object and then rename it spot.

There is one issue that requires special attention. This article is meant to explore the issue of cloning/copying objects. To accomplish this, a simple example is presented. However, there are always alternate means to accomplish the same task.

Page 1 of 5

This article was originally published on July 6, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date