Skip to content

Instantly share code, notes, and snippets.

@NAVNEETOJHA
Created March 23, 2021 12:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NAVNEETOJHA/90451beae05c5e3a1d2fc552898c4c28 to your computer and use it in GitHub Desktop.
Save NAVNEETOJHA/90451beae05c5e3a1d2fc552898c4c28 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
// Cloneable is a marker interface
class Address implements Cloneable {
public String streetName;
public int houseNumber;
public Address(String streetName, int houseNumber)
{
this.streetName = streetName;
this.houseNumber = houseNumber;
}
@Override
public String toString()
{
return "Address{" +
"streetName='" + streetName + '\'' +
", houseNumber=" + houseNumber +
'}';
}
// base class clone() is protected
@Override
public Object clone() throws CloneNotSupportedException
{
return new Address(streetName, houseNumber);
}
}
class Person implements Cloneable
{
public String [] names;
public Address address;
public Person(String[] names, Address address)
{
this.names = names;
this.address = address;
}
@Override
public String toString()
{
return "Person{" +
"names=" + Arrays.toString(names) +
", address=" + address +
'}';
}
@Override
public Object clone() throws CloneNotSupportedException
{
return new Person(
// clone() creates a shallow copy!
/*names */ names.clone(),
// fixes address but not names
/*address */ // (Address) address.clone()
address instanceof Cloneable ? (Address) address.clone() : address
);
}
}
class CloneableDemo
{
public static void main(String[] args)
throws CloneNotSupportedException
{
Person john = new Person(new String[]{"John", "Smith"},
new Address("London Road", 123));
// shallow copy, not good:
//Person jane = john;
// jane is the girl next door
Person jane = (Person) john.clone();
jane.names[0] = "Jane"; // clone is (originally) shallow copy
jane.address.houseNumber = 124; // oops, also changed john
System.out.println(john);
System.out.println(jane);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment