Question: A novice programmer writes the following code in order to be able to completely clone an object of type Car. public class Tyre{ private int
A novice programmer writes the following code in order to be able to completely clone an object of type Car.
public class Tyre{ private int treadRemaining; public void setTread(int t){ treadRemaining = t; } public int getTread(){ return treadRemaining; } }
public class Car extends Vehicle implements Cloneable{ private Tyre tyres[] = new Tyre[4]; public Car(){ for(int i = 0; i < 4; i++) tyres[i] = new Tyre(); } } public Object clone() throws CloneNotSupportedException{ Car c = new Car(); c.tyres = this.tyres; return c; } }
d. Rewrite the code for clone() method to address the problem you have identified and allow Car objects to be fully cloned i.e. allow deep copy.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
