Try โ€‚โ€‰HackMD

Shallow copy vs Deep Copy

memento: a saved snapshot of the state of an object for possible later use
Shallow copy: copy just primitive data types

  1. make the class implements the clonable interface
  2. override method clone (Make it public)
  3. when you want to copy an object just call the clone method

Deep copy: copy the object with all inner objects
how do we can perform deep copy?
by saving the object into a file and then read into another object.
4. make the class and all inner classes implements Serializable interface.

// write
โ€‹โ€‹FileOutputStreem fos = new FileOutputStreem();
โ€‹โ€‹ObjectOutputStreem oos = new ObjectOutputStreem (fos);
oos.writeObject(object1);

// read
FileInputStreem fis = new FileInputStreem (path);
ObjectInputStreem ois = new ObjectInputStreem (fis)
Object2 object2 = ois.readObject();

if you have some attribute you don't want to copy use "transient" keyword