title: Writing Java constructor for child class that takes parent object
body: <p>Is it possible to use a parent object to instantiate a child object? The class Length2 extends Length1 with the addition of an instance variable. Common I tried to make a copy constructor that takes the Length1 as a param and sets the extra ivar to 0, but I'm still being told I cannot convert from Legnth1 to Length2. I suppose there may be some implicit casting taking place... have a look.</p>
  
  <pre><code>public class Length1 extends Length implements Comparable&lt;Length1&gt;{
  
      protected int miles;
      protected int yards;
  
      public Length1(){ //default constructor
          miles = 0;
          yards = 0;
      }
  
      public Length1(int m, int y){ //constructor
          miles = m;
          yards = y;
      }
  
      public void setYards(int x){
          yards = x;
      }
      public void setMiles(int x){
          miles = x;
      }
      public int getYards(){
          return yards;
      }
      public int getMiles(){
          return miles;
      }
  </code></pre>
  
  <p>...</p>
  
  <pre><code>public class Length2 extends Length1 {
  
      protected int feet;
  
      public Length2(){ //default constructor
          super(0,0);
          setFeet(0); 
      }
      public Length2(int m, int y, int f){ // constructor
          super(m,y); 
          setFeet(f); 
      }
      public Length2(Length1 x){ 
          super(x.miles,x.yards);
          setFeet(0); 
      }
      public void setFeet(int x){
          feet = x;
      }
      public int getFeet(){
          return feet;
      }
  </code></pre>
  
  <p>...</p>
  
  <pre><code>public class LengthTest{
  
      public static void main(String[] args) {
          Length1 a,b,c,d,e,f;
          Length2 g,h,i,j;
  
          a = new Length1(78,1610);
          b = new Length1(77,1694);
          c = new Length1();
          d = new Length1();
  
          g = new Length2(32,1022,1);
          h = new Length2(31,1700,2);
          i = new Length2();
          j = new Length2();
  
  
          j = c; //problem occurs here
  
  
      }
  
  }
  </code></pre>