0

I need to make a class called Garage implement Cloneable and override the Object.clone() method to make a deep copy of an object of type Garage which contains an array of objects of type Vehicle.

I have read on several SO answers that Cloneable is broken and it is wise not to use it, but my lab assignment demands that I implement Cloneable and code a proper clone() method. I have been looking for long but still haven't been able to do this. Any help would be much appreciated.

This is my code for the Garage class:

public class Garage extends Vehicle implements Cloneable {
    // array to store up to 15 Vehicle objects
    Vehicle array[] = new Vehicle[15];

    // overriding the clone method to perform a deeper copy
    @Override
    protected Object clone() throws CloneNotSupportedException {
        //TODO
    }
}

This is my code for the Vehicle class:

public class Vehicle {
    int numWheels;
    int ID;
    String name;
}

1 Answer 1

1

First of all Garage extends Vehicle makes no sense. I'm assuming it's a mistake. A Garage is not a Vehicle.

In Garage you can implement clone as :

@Override
protected Object clone() {
    Garage other = new Garage ();
    for (Vehicle v : this.array) {
        other.addVehicle((Vehicle)v.clone());
    }
    return other;
}

Then you have to implement clone() in the Vehicle class hierarchy.

Sign up to request clarification or add additional context in comments.

6 Comments

How should I then implement the addVehicle() method? The addVehicle method should add a Vehicle to the array of the Garage object, for which we need to pass a Vehicle to this method, but the line of code inside your for loop is passing an Object, not a Vehicle. It's giving me a type mismatch.
@AkshayDamle Sorry about that. You should cast the result of the clone to the required type. Edited my answer.
The better way is to get the object with super.clone() rather than new Garage(); the former will work with subclasses that inherit this method, the latter will not.
@newacct I assumed that Garage shouldn't be a sub-class of Vehicle (since it makes no sense), so there is no super.clone(). Even if there was, you wouldn't want to get the instance from the super class, since that would give you an instance of the super class. In that case I would split the cloning to two methods - one would just create an instance of the cloned object and call the other method, which would clone the members. The first one would be overridden by each class. The second would be overridden by each class, but would also call its super-class implementation.
@Eran: No, there is always a super.clone(), because there is Object.clone(). And the point of Object.clone() is that it gives you an instance of the same class as the object, whatever the class is. This you cannot do with new Whatever.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.