Source Code : A Cloning Example
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
A Cloning Example
public class MainClass {
public static void main(String[] args) {
Employee emp1 = new Employee("M", "A");
emp1.setSalary(40000.0);
Employee emp2 = (Employee) emp1.clone();
emp1.setLastName("Smith");
System.out.println(emp1);
System.out.println(emp2);
}
}
class Employee {
private String lastName;
private String firstName;
private Double salary;
public Employee(String lastName, String firstName) {
this.lastName = lastName;
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public Double getSalary() {
return this.salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Object clone() {
Employee emp;
emp = new Employee(this.lastName, this.firstName);
emp.setSalary(this.salary);
return emp;
}
public String toString() {
return this.getClass().getName() + "[" + this.firstName + " " + this.lastName + ", "
+ this.salary + "]";
}
}
Thank with us