class Flea implements Cloneable {
public Flea() {
}
public void setName(String aName) {
name = aName;
}
public String getName() {
return name;
}
public String getSpecies() {
return species;
}
public void sound() {
System.out.println("Psst");
}
public String toString() {
return super.toString() + "\nIt's " + name + " the " + species;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
private String name;
private String species;
}
public class MainClass {
public static void main(String[] args) {
try {
Flea myPet = new Flea();
myPet.setName("my name");
Flea yourPet = (Flea) myPet.clone();
yourPet.setName("Gnasher");
System.out.println("\nYour pet details:\n" + yourPet);
System.out.println("\nMy pet details:\n" + myPet);
} catch (CloneNotSupportedException e) {
e.printStackTrace(System.err);
}
}
}
|