blog

Home / DeveloperSection / Blogs / Creating a Copy Constructor

Creating a Copy Constructor

zack mathews1410 19-May-2016

One more benefit of constructor overloading is the ability to create a copy constructor, which allows us to create an object copy from the values of another similar object. This gives us two independent objects to work with, and changes made to one will not affect the other. The example shown here explains how to write a copy constructor.

public class TimeOfDay { 
                     private int hour, mins;
                     public TimeOfDay(int hour, int mins) {
                             this.hour = hour;
                             this.mins = mins;                      }
                     public TimeOfDay(TimeOfDay other) {
                             this(other.hour, other.mins);
                     }
                     public String toString() {
                             return (this.hour + ":" + this.mins);
                     }
                     public static void main(String[] args) {
                             TimeOfDay currentTime = new TimeOfDay(12, 30);
                             TimeOfDay copyOfCurrentTime = new TimeOfDay(currentTime);                              System.out.println("Original: " + currentTime.toString());                              System.out.println("Copy: " + copyOfCurrentTime.toString());                      }
}

The class TimeOfDay defines two constructors—one that takes two arguments of the integer type and one that takes an object reference of the same type. The first constructor accepts the values of hour and mins from the user and initializes the created object’s state with these values.

The second constructor calls this two-argument constructor with the help of the this keyword, passing the state of the object that is passed to it as a parameter. The main program creates two objects using these two constructors and prints their state on the user’s console to verify that both have the same state.

To print the state of the TimeOfDay object, we override the toString method of the Object class. Remember that every class in Java inherits from Object, which is a top-level class. In the overridden toString method, we format a string containing the state values of the current object and return it to the caller.

The default implementation of the toString method returns an object reference. Many times, developers override this method to print the object’s state in the desired format.

Invoking Constructor Overview:

So far, we have overloaded constructors to provide default values to some of the fields of a class, to provide a different initialization, and to create a copy constructor. Here are the key points to remember:

·To invoke a parent constructor, we must place a call to super in the first line of the constructor. Calling super after the first statement results in an error.

·The parameters we pass to super decide which specific implementation of the parent class constructor is called.

·The first statement in the constructor may be a call to super or this. We use super to call the superclass constructor and we use this to call some other constructor defined within the same class.

·If no this or super call is used in a constructor, the compiler adds an implicit call to super(), which calls the parent “no-arguments” constructor. The no-arguments constructor may be implicit or explicitly declared. If no such constructor is available in the parent constructor, the compiler generates an error.

·The compiler does not provide a no-arguments default constructor if we write our own constructor (either a no-arguments constructor or a constructor with arguments).


Updated 15-Mar-2018

Leave Comment

Comments

Liked By