Implements clone Method - Java Object Oriented Design

Java examples for Object Oriented Design:Clone

Description

Implements clone Method

Demo Code

public class CloneTest
{
    public static void main(String[] args)
    {//from   ww  w  .j av  a2s.  c  o m
    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 + "]";

  }

}

Related Tutorials