title: Downcasting instance from external Jar
body: <pre><code>//Class defined in external jar
  class A{
      many methods...
      public getId() {};
  }
  
  //I want to extends this class and overwrite single method
  class MyA extends A{
  private int myId;
  public getId() {return myId};
  }
  
  void main ()
  {
    A a = Factory.getA(); //External class create the instance
    MyA mya = (MyA)a;     //runtime Error!! I want to convert A to myA
  }
  </code></pre>
  
  <p>Hi,
  I want to extends an instance which I get from external Jar and overwrite a single method getId(). I don't control the creation of the instance so the only solution I got was to pass it to my constructor and init all members manually, example here:</p>
  
  <pre><code>class MyA extends A{
  private int myId;
  public MyA(A a, int myId)
  {
      this.myId = myId;
      //init all other methods from a.? to this.?
      this.setXXX(a.getXXX());
      this.setYYY(a.getYYY());
      ....many methods...
  }
  public getId() {return myId};
  }
  </code></pre>
  
  <p>Is there a better way?</p>