Mega Code Archive

 
Categories / Java / Class
 

Class is declared to be cloneable

class Employee implements Cloneable {   String name;   int salary;   public Employee(String name, int salary) {     this.name = name;     this.salary = salary;   }   public Employee() {   }   public String getName() {     return name;   }   public void setName(String name) {     this.name = name;   }   public void setSalary(int salary) {     this.salary = salary;   }   public int getSalary() {     return this.salary;   }   public Object clone() throws CloneNotSupportedException {     try {       return super.clone();     } catch (CloneNotSupportedException cnse) {       System.out.println("CloneNotSupportedException thrown " + cnse);       throw new CloneNotSupportedException();     }   } } public class Main {   public static void main(String[] args) {     try {       Employee e = new Employee("Dolly", 1000);       System.out.println(e);       System.out.println("The employee's name is " + e.getName());       System.out.println("The employees's pay is " + e.getSalary());       Employee eClone = (Employee) e.clone();       System.out.println(eClone);       System.out.println("The clone's name is " + eClone.getName());       System.out.println("The clones's pay is " + eClone.getSalary());     } catch (CloneNotSupportedException cnse) {       System.out.println("Clone not supported");     }   } }