title: Deep copy of Object array that includes multiple classes in Java?
body: <p>I'm currently struggling to create a deep copy of my Object array in Java. This Object array includes values from three different classes, with only one of these classes being a subclass of the other. The classes are listed as follows:</p>
  <pre class="lang-java prettyprint-override"><code>//First superclass
  package Plane;
  public class Plane {
      
      
       private String brand;
       private double price; 
       private int horsepower;
      
       
      public Plane() {
          brand = &quot;test&quot;;
          price = 50000.99; 
          horsepower = 500;
      }
      
      public Plane(String planeBrand, double planePrice, int planePower) {
          this.brand = planeBrand;
          this.price = planePrice;
          this.horsepower = planePower;
      }
      
      public String getBrand() {
          return this.brand;
      }
      public void setBrand(String planeBrand) {
          this.brand = planeBrand;
      }
      
      public double getPrice() {
          return this.price;
      }
      
      public void setPrice(double planePrice) {
          this.price = planePrice;
      }
      
      public int getPower() {
          return this.horsepower;
      }
      
      public void setPower(int planePower) {
          this.horsepower = planePower;
      }
      
      //COPY CONSTRUCTOR
      public Airplane(Plane plane) {
          this.brand = plane.getBrand();
          this.price = plane.getPrice();
          this.horsepower = plane.getPower();
      }
      
      public String toString() {
          return &quot;The airplane is manufactured by &quot; + this.brand + &quot; and costs $&quot; + this.price + &quot;. It has &quot; + this.horsepower + &quot; horsepower.&quot;;
      }
  
      public boolean equals(Plane plane) {
          if (!(plane instanceof Plane) || plane == null) {
                  return false;
              } else if (this.brand != plane.getBrand() || this.price != plane.getPrice() || this.horsepower != plane.getPower()) {
                      return false;
                  } else {
                      return true;
                  }
              }
          }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>//Second class
  package Boat;
  public class Boat {
  
      private double weight;
      private double price;
      
      public Boat() {
          weight = 3949.5;
          price = 64000;
      }
      
      public Boat(double boatWeight, double boatPrice) {
          weight = boatWeight;
          price = boatPrice;
      }
      
      public double getWeight() {
          return this.weight;
      }
      
      public void setWeight(double boatWeight) {
          this.weight = boatWeight;
      }
      
      public double getPrice() {
          return this.price;
      }
  
      public void setPrice(double boatPrice) {
          this.price = boatPrice;
      }
      
      //COPY CONSTRUCTOR
      public Boat(Boat boat) {
          this.weight = boat.getWeight();
          this.price = boat.getPrice();
      }
      
      public String toString() {
          return &quot;This boat weighs &quot; + this.getWeight() + &quot;kg and costs $&quot; + this.getPrice() + &quot;.&quot;;
      }
      
      public boolean equals(Boat boat) {
          if (!(boat instanceof Boat) || boat == null) {
              return false;
          } else if (this.price != boat.price || this.weight != boat.weight) {
              return false;
          } else {
              return true;
          }
      }
      
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>//Third class which extends Plane
  package Heli;
  import Plane.Plane;
  
  public class Heli extends Plane{
      
      private int cylinders;
      private int year;
      private int capacity;
      
      public Heli() {
          cylinders = 6;
          year = 2018;
          capacity = 35;
      }
  
      public Heli(String planeBrand, double planePrice, int power, int cylinders, int year, int capacity) {
          super(planeBrand, planePrice, power);
          this.cylinders = cylinders;
          this.year = year;
          this.capacity = capacity;
      }
  
      public int getCylinders() {
          return this.cylinders;
      }
      
      public void setCylinders(int cylinders) {
          this.cylinders = cylinders;
      }
      
      public int getYear() {
          return this.year;
      }
      
      public void setYear(int year) {
          this.year = year;
      }
      
      public int getCapacity() {
          return this.capacity;
      }
      
      public void setCapacity(int capacity) {
          this.capacity = capacity;
      }
      
      //COPY CONSTRUCTOR
      public Helicopter(Helicopter helicopter) {
          super(helicopter);
          this.cylinders = helicopter.cylinders;
          this.year = helicopter.year;
          this.capacity = helicopter.capacity;
      }
      
      public String toString() {
          return &quot;This heli is manufactured by &quot; + super.getBrand() + &quot; and costs $&quot; + super.getPrice() + &quot;. It has &quot; + super.getPower() + &quot; horsepower. It was manufactured in &quot; + getYear() + &quot;, has &quot; + getCylinders() + &quot; cylinders and has a total carrying capacity of &quot; + getCapacity() + &quot; lbs.&quot;;
      }
  
      public boolean equals(Heli heli) {
          if (!(heli instanceof Heli) || heli == null) {
              return false;
          } else if (this.cylinders != heli.getCylinders() || this.year != heli.getYear() || this.capacity != heli.getCapacity() || !(super.equals(heli))) {
              return false;
          } else {
              return true;
          }
      }
      
  }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>//Driver class where my copy takes place
  public class Driver {
  
  
      private static Object[] newMixedArray = new Object[3];
  
      //THIS IS WHERE MY COPY ARRAY NEEDS TO BE IMPLEMENTED
      public static void copyObjects(Object[] newMixedArray) {
          Object[] copyArray = new Object[newMixedArray.length];
      
      }
  
  
      public static void main(String[] args) {
          
          newMixedArray[0] = new Plane(&quot;Bombardier&quot;, 50000000, 4000);
          newMixedArray[1] = new Boat(&quot;Boeing&quot;, 8500000.75, 8500);
          newMixedArray[2] = new Heli(&quot;Glastron&quot;, 250500.99, 837, 8, 2020, 35);
          
  
          copyObjects(newMixedArray);
  
      }
  
  }
  </code></pre>
  <p>Now the kicker is that I cannot use clone() to create my copy array. At first, I created a simple loop to copy my original array into another array called &quot;copyArray&quot; and print out the values, however it came to my attention that this was simply a shallow copy and not a deep copy. I've been wracking my brain for the last several days trying to find a solution but have come up empty, especially since most answers I've found online don't seem to apply to situations with multiple classes.</p>