What is a Copy Constructor?
Learn what a copy constructor is, shallow vs deep copy, and how C++ and Java differ, with a worked Java code example.
Expected Interview Answer
A copy constructor is a special constructor that creates a new object as a duplicate of an existing object of the same class, copying its field values into the new instance.
In C++, the compiler generates a default copy constructor that performs a shallow, member-wise copy unless one is explicitly defined; classes managing raw pointers or dynamic memory must write their own to perform a deep copy and avoid two objects sharing (and eventually double-freeing) the same resource. Java and most other object-oriented languages have no built-in copy constructor syntax; developers write one manually as a regular constructor accepting an instance of the same class and copying its fields, or use Object.clone() with its own caveats. The core distinction that matters in interviews is shallow copy (copies references, both objects share nested objects) versus deep copy (recursively duplicates nested objects too).
- Creates independent copies safe from shared-state mutation bugs
- Deep copy avoids two objects owning the same dynamic resource
- Explicit copy logic documents exactly what "copying" means for the class
- Prevents accidental double-free/dangling-pointer bugs in C++
AI Mentor Explanation
A copy constructor is like a scout duplicating a player’s entire skill profile onto a new draft card — batting average, bowling style, fielding rating — so the new card is independent and can be edited without touching the original player’s record. A shallow copy would be like copying only the injury-report reference, so editing the new card’s injury note accidentally changes the original player’s file too. A deep copy duplicates that nested injury record fully, giving the new card true independence.
Step-by-Step Explanation
Step 1
Accept an instance of the same class
Define a constructor taking a parameter of the same class type (often by reference in C++).
Step 2
Copy primitive fields directly
Assign value-type fields straight across — these are safe to copy by value.
Step 3
Deep-copy reference/pointer fields
For fields pointing to other objects or dynamic memory, create new copies of those nested objects rather than copying the reference.
Step 4
Validate independence
Mutate the copy and confirm the original object’s state is unaffected.
What Interviewer Expects
- A clear definition with correct C++ vs. Java framing
- Shallow copy vs. deep copy distinguished with a concrete example
- Awareness of C++’s implicitly generated copy constructor and its dangers with pointers
- Knowledge of alternatives like Java’s Object.clone() or copy factory methods
Common Mistakes
- Claiming Java has copy constructor syntax identical to C++
- Not distinguishing shallow copy from deep copy
- Forgetting the C++ default copy constructor performs a shallow, member-wise copy
- Missing the double-free/dangling-pointer risk from shallow-copying raw pointers
Best Answer (HR Friendly)
“A copy constructor builds a new object by duplicating the values from an existing object of the same type. The key nuance is shallow versus deep copying — a shallow copy just copies references to nested objects, so both objects end up sharing state, while a deep copy duplicates those nested objects too, so the new copy is fully independent.”
Code Example
class Address {
String city;
Address(String city) { this.city = city; }
Address(Address other) { this.city = other.city; } // copy nested object
}
class Person {
String name;
Address address;
Person(String name, Address address) {
this.name = name;
this.address = address;
}
// Copy constructor: deep-copies the Address field
Person(Person other) {
this.name = other.name;
this.address = new Address(other.address); // deep copy
}
}
Person original = new Person("Alice", new Address("Chennai"));
Person copy = new Person(original);
copy.address.city = "Bengaluru";
System.out.println(original.address.city); // still "Chennai" -> independent copyFollow-up Questions
- What is the difference between a shallow copy and a deep copy?
- How does C++’s implicitly generated copy constructor behave with raw pointers?
- How does Object.clone() in Java differ from a manually written copy constructor?
- When would you use a copy factory method instead of a copy constructor?
MCQ Practice
1. A shallow copy of an object containing a reference field results in?
Shallow copy duplicates the reference value, not the object it points to, so both objects share the same nested instance.
2. In C++, the compiler-generated default copy constructor performs?
The implicit C++ copy constructor copies each member field by value, which is shallow for pointer/reference members.
3. Java classes get a copy constructor automatically like C++ does. True or false?
Java has no implicit copy constructor; developers write one manually or override Object.clone().
Flash Cards
What is a copy constructor? — A constructor that creates a new object by duplicating the field values of an existing object of the same class.
Shallow copy vs. deep copy? — Shallow copy duplicates references (shared nested objects); deep copy duplicates the nested objects themselves (fully independent).
Does Java have copy constructor syntax like C++? — No — Java has no built-in copy constructor; developers write one manually or use Object.clone().
Why write a custom copy constructor in C++? — To perform a deep copy and avoid two objects sharing (and later double-freeing) the same dynamic resource.