import java.io.*;
import java.util.*;

public class A implements Cloneable, Serializable
{
  public Vector v = new Vector(10);

  public A(Vector v)
  {
    this.v = v;
  }

  // "deep copy"

  public Object deepClone()
  {
    try
    {
      // écriture
      ByteArrayOutputStream b = new ByteArrayOutputStream();
      ObjectOutputStream out = new ObjectOutputStream(b);
      out.writeObject(this);
      out.close();

      // lecture
      ByteArrayInputStream clone = new ByteArrayInputStream(b.toByteArray());
      ObjectInputStream in = new ObjectInputStream(clone);
      return in.readObject();

    } catch (Exception e) {
      e.printStackTrace();
    }

    return null;
  }

  // "shallow copy"

  public Object clone()
  {
    try
    {
      return super.clone();
    } catch (Exception e) {
      e.printStackTrace();
    }

    return null;
  }

  public static void main(String[] args)
  {
    A a = new A(new Vector(10));

    ////////// SHALLOW COPY
    // A b = (A) a.clone();

    ////////// DEEP COPY
    A b = (A) a.deepClone();

    System.out.println("A : " + a.v.size());
    b.v.addElement("Hello world !");
    System.out.println("B : " + b.v.size());
    System.out.println("  + " + b.v.elementAt(0));
    System.out.println("A : " + a.v.size());
    System.out.println("  + " + a.v.elementAt(0));
  }
}
