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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!