I'm trying to get a clone of an object using copy-contructor, unfortunately seems that all the modification to the clone are reflected to the original object.
I have this interface
public interface Car{
public Car newInstance(Car c);
}
and various Object hat implements the interface in this way
public FerrariSpyder implements Car{
String name;
String description
ArrayList<Feature> featureList;
public FerrariSpyder(String name, String description, ArrayList<Feature> featureList){
this.name=name;
this.descripion=descripion;
this.featureList=featureList;
}
@Override
public Car newInstance(Car c) {
return FerrariSpyder.newInstance((FerrariSpyder) c);
}
public static FerrariSpyder newInstance(FerrariSpyder fs) {
return new FerrariSpyder(fs.getName(), fs.getDescription(), fs.getFeatureList());
}
}
Now assuming that I have a Car
object named originalCar
I try to clone this with
Car clone=originalCar.newIstance(originalCar);
If I add elements in featureList
of the clone also the featureList
in originalCar
will be modified.