Skip to content

Instantly share code, notes, and snippets.

@MykolaBova
Last active August 29, 2015 14:06
Show Gist options
  • Save MykolaBova/7b8faf29d4cbc8a72cdc to your computer and use it in GitHub Desktop.
Save MykolaBova/7b8faf29d4cbc8a72cdc to your computer and use it in GitHub Desktop.
ClonePerson1 First example of cloneable object
public class ClonePerson1 implements Cloneable{
private String name;
public ClonePerson1(String name) {
this.name = name;
}
public String getName() {
return name;
}
protected ClonePerson1 clone() {
try {
return (ClonePerson1) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
public static void main(String[] args) {
ClonePerson1 p = new ClonePerson1("Sam");
ClonePerson1 pClone = p.clone(); // Can you do this, if clone method in Person class is not public
System.out.println(pClone.getName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment