001package clone.magic; 002public class Main { 003 public static void main(String[] argv) throws CloneNotSupportedException { 004 B x = new B(42,27); 005 System.out.println(x); 006 System.out.println(new A(x)); 007 System.out.println(x.copy()); 008 System.out.println(x.clone()); 009 } 010} 011 012class A implements Cloneable { 013 int i; 014 public A(int i) { this.i = i; } 015 016 // A copy constructor 017 public A(A that) { this.i = that.i; } 018 019 // A copy method 020 public Object copy() { return new A(i); } 021 022 // The clone method 023 public Object clone() throws CloneNotSupportedException { 024 return super.clone(); 025 } 026 public String toString() { return "A("+i+")"; } 027} 028 029class B extends A { 030 int j; 031 public B(int i, int j) { super(i); this.j = j; } 032 public String toString() { return "B("+i+","+j+")"; } 033}