August 19, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Object Integrity & Security: Duplicating Objects: Part 2

  • June 11, 2007
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

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.

Click here to start at the beginning of the series.

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).

java.lang Class Object

java.lang.Object

public class Object

Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

Since:

JDK1.0

See Also:

Class

Method Summary
protected Object
clone()

Creates and returns a copy of this object.

http://java.sun.com/j2se/1.5.0/docs/api/index.html

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.

// Class Dog
class Dog implements Cloneable {

   String name;
   String breed;

   public Dog(String n, String b) {

      name  = n;
      breed = b;
   }

   public Object clone() {
      try {
         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;

   }

}

Listing 1: The Dog Class with the clone() Override

Also note the casting as it appears in Listing 2. When the clone() method returns the object, the object in this case is of type Dog. Thus, you need to cast the return to the type Dog.

// Class Duplicate
public class Duplicate {

   public static void main(String[] args) {

      Dog fido = new Dog("fido", "retriever");

      Dog spot;

      spot = (Dog) fido.clone();

      System.out.println("name = " + fido.getName());
      System.out.println("name = " + spot.getName());


   }
}

Listing 2: The Duplicate Class with the clone() method

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)





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel