July 21, 2019
Hot Topics:

Object Integrity & Security: Duplicating Objects

  • May 2, 2007
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

Cloning Objects

What do you have to do to actually duplicate an object and create a duplicate instance? You could create a second object by hand. In the code below, you create a reference called fido1 and one called fido2.

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

There may be many reasons why you would want to duplicate objects. You may want to do some sort of conversion where you need most of the original information. Or, you may want to create a new object for another Dog that will contain most, but not all, of the original information. The problem with this approach is that all of the updated information must be set by hand. For example, if you want to change the breed type, you would have to explicitly code that.

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 what the definition for clone() looks like (remember that all Java objects inherit from the object class).

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


See Also:
Method Summary
protected  Object clone()
Creates and returns a copy of this object.


Using the clone() method does take a bit of work. In Listing 03, you add the line of code to clone the object (see the line in red).

// Class Duplicate
public class Duplicate {

   public static void main(String[] args) {

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

      Dog spot;

      spot = fido.clone();

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


Listing 03: The Duplicate Class

However, when you compile this code, you run into some tricky issues. As you can see in Figure 03, the coding involved is not totally straightforward. Specifically, you have two issues that you have to deal with. The first is the fact that the clone() method in the object class is defined as protected. The second comes into play because the compiler complains that the cloning procedure is dealing with incompatible types.

Figure 03: Attempting to Clone an Object

The first issue can be dealt with by overriding the clone() method in the Dog class itself. In fact, the clone() method is meant as a standard interface and not a complete solution to object duplication. Thus, like constructors, you probably want to create your own version of the clone() method.

In Listing 04, you add the code for the localized clone() method override in red. You will explore this code in much more detail in the next column; however, 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.

// 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 04: The Dog Class with the clone() Override

Page 3 of 4

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