Its like a child is trying to create its parent and it wants the parent to be just like itself!!!
The problem here is that you are trying to expand the limits of "a" at runtime.
Pratik D mehta wrote:
but In the next example .
You are doing the same thing .
(testclone)super.clone // which returns instance of type Object
and than you are assigning it to refrence x2 which is refrence of testclone (Child class).
this works !!
But it should not ?
I want to understand this
first see here..
protected Object clone()
Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object focus on the word "this" which means current object not a object of a class Object
So there is no problem in this line
because super.clone() is returning a object of the type "testclone"
i cannot understand why you are having a problem with this
because x1.clonetest(); will return a object of the type testclone and it should have no problem while assigning to its own refernce type.
Shanky Sohar wrote:what i think is
super.clone() return a shallow copy of the current object.
in order to get a deep copy we have to do the type casting..
By this you meant that
If a shalllow copy of object is created we will have problems like .
1) If a (shallow) copy of class object is than it would contain refrences of objects which point to the same original object when their copy is made.
So if we change the value of instance variable in the copy of the object it would change the value in the original object.
2) If we clone a object and it contains a reference to the I/O Stream than the copy would also contain the reference to the same stream.
And if we deep copy it
The object copied would contain references to the new objects, rather than the original.
Please give a nodd at this , else give me an example of deep copy and shallow copy
Thankyou Shanky sohar