I'm cloning one object into another object and then trying to change only two parameters in new object. But still the original object is getting changed. I just want both copies should be separate. Here is my code
Subgroup sg1 = new Subgroup();
sg1.setFname("Vali");
sg1.setMname("Sheik");
sg1.setLname("Sha");
Group g1 = new Group();
g1.setSg(sg1);
try {
Group g2 = (Group) g1.clone();
Subgroup sg1 = g2.getSg();
sg2.setFname("parvez");
sg2.setMname("syed");
sg2.setLname("khan");
g2.setSg(sg2);
System.out.println(g1);
System.out.println(g2);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
Both cases it's printing first object only.
Clone method in group class
Protected Object clone() throws CloneNotSupportedException {
return super.clone();
}