Last active
August 29, 2015 14:06
-
-
Save MykolaBova/7b8faf29d4cbc8a72cdc to your computer and use it in GitHub Desktop.
ClonePerson1 First example of cloneable object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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