Copying Objects
Clone() Recipe
1. implements Cloneable 2. Make a public clone() method 3. Call super.clone() to get a bit for bit copy 4. Set any state in your own class
What is a copy constructor?
A constructor that takes in an object of the same type and manually copies the data, create a copy doing e.g. *Vehicle vcopy = new Vehicle(v)"
What is a marker interface?
An empty interface that is used to label classes
What is Cloneable?
An interface in Java - anything you clone needs to extend this interface (e.g. Vehicle implements Cloneable)
Every object has the clone() method in Java, but what activates its ability to use this method?
If it implements the Cloneable interface
Every class inherits from Object, which has a clone() method. Why can this go wrong when copying objects?
Only the primitives are actually copied across. Any reference types (arrays, objects, etc) are not.
Why do you need to cast the clone return in older versions of Java?
Our implementation of clone() returns an Object (to override the clone() method in Object which returns an Object)
What type of interface is Cloneable?
A marker interface - is empty because the clone() method is already inherited from Object so no need to specify it
What is the disadvantage of using copy constructors?
Can't cope with parent cast (i.e. cast up the tree) so can't make a polymorphic copy -> copy can't access private state from the parent class of the thing you're copying, so that stuff doesn't get copied across, meaning you can't cast the copy to its parent type.
When we want to actually copy an object rather than its memory address, what do we do?
Clone it
What do recent versions of Java allow in respect to the return type of clone?
Don't need to cast the clone return as recent version of Java allow you to override a method in a subclass and change its return type to a subclass of the original class return type for the object being cloned i.e. if original method returns Object, overriding method can return any type that inherits from Object
How do you deep copy an array?
Have to create a new array and copy each element across by setting element to the same value at corresponding index in original array
How do you deep copy an object that contains (non-array) reference types?
Inside clone() method for that object, call super.clone() and store under a variable casted to type of object you're cloning, then clone reference types stored in that cloned object.
When cloning, why don't you need to set state for primitives?
Primitives sit inside the object you're cloning and is copied bit by bit to the new clone when you call super.clone(), whereas the references are copied over for any objects inside the object you're cloning
What type of copy does the default implementation of a clone() method perform - shallow or deep?
Shallow
Doing <array>.clone() deep copies or shallow copies the original array?
Shallow copies
Deep copy
The actual underlying object is copied across by making a new object
What is the defacto copying approach in OOP languages, since clone() is unique to Java?
Using a copy constructor
Shallow copy
When the reference for a reference type is copied across rather than the actual underlying object/array/etc itself