I tried to clone an object and change member functions of new clonned object. If it is shallow copy and according to wiki page, shallow copy and original object point to same object then , it should change the variable value.
CloneExample obj1=new CloneExample();
CloneExample obj2=(CloneExample) obj1.clone();
obj1.a=2;
obj1.c='a';
System.out.println("obj1 real "+obj1.a+" "+obj1.c);
System.out.println("obj2 real "+obj2.a+" "+obj2.c);
obj2.a=99;
obj2.c='z';
System.out.println("obj2 after change "+obj2.a+" "+obj2.c);
System.out.println("obj1 after change "+obj1.a+" "+obj1.c);
System.out.println("obj1="+obj1+" obj2="+obj2);
If this clone is shallow copy, then obj2 and Obj1 point to same reference obj, altering obj2 should reflect in obj1. It doesn't happen. Can any one explain (I understand clone is designed to give us copy of object, but if it is shallow copy then why we need shallow copy?, we should always go for deep copy). Thank you.