Java Cloneable interface implement clone() method

Introduction

The following example overrides clone().

We can call the clone() method from code outside of its class.

// Override the clone() method.
class TestClone implements Cloneable {
  int a;//from  w ww  . j a va2s  .  c  om
  double b;

  // clone() is now overridden and is public.
  public Object clone() {
    try {
      // call clone in Object.
      return super.clone();
    } catch(CloneNotSupportedException e) {
      System.out.println("Cloning not allowed.");
      return this;
    }
  }
}

public class Main {
  public static void main(String args[]) {
    TestClone x1 = new TestClone();
    TestClone x2;

    x1.a = 10;
    x1.b = 20.98;

    // here, clone() is called directly.
    x2 = (TestClone) x1.clone(); 

    System.out.println("x1: " + x1.a + " " + x1.b);
    System.out.println("x2: " + x2.a + " " + x2.b);
  }
}



PreviousNext

Related