Object Cloning
Making exact copies of objects.
Object Cloning
Analogy: concept
Cloning is like Photocopying:
- Shallow Copy: You photocopy the main page, but if there's a sticky note attached, you just point to the same sticky note. If someone writes on the sticky note, both copies see it.
- Deep Copy: You photocopy the main page AND the sticky note. Now you have two completely separate sets.
The Cloning Machine
See the difference between Shallow and Deep Copy when we mutate shared data (DNA).
Object Cloning (Shallow vs Deep)
🐑
Original
DNA: ATCG
Empty
Shallow Copy: Copies the object, but nested objects (like DNA) are shared references. Changing the original's DNA changes the clone's DNA too!
Key Concepts
-
Shallow Copy: Creates a new object, but inserts references into it to the objects found in the original.
- Fast, but risky if mutable objects are shared.
- Default behavior of
Object.clone()in Java.
-
Deep Copy: Creates a new object and recursively copies everything found in the original.
- Slower, but safer. The two objects are fully independent.
The Code
class Sheep implements Cloneable {
String name;
DNA dna; // A mutable object
// Shallow Copy (Default)
public Object clone() {
return super.clone();
}
// Deep Copy (Manual)
public Sheep deepClone() {
Sheep copy = new Sheep();
copy.name = this.name;
copy.dna = new DNA(this.dna.code); // New object!
return copy;
}
}
Up Next
Serialization