Java: super.clone() method and inheritance

Viewed 20139

I have a quick question regarding the clone() method in Java, used as super.clone() in regard to inheritance - where I call the clone() method in the parent class all the way up from the button.

The clone() method is supposed to return a copy of this object, however if I have three classes in an inheritance heirachy and call super.clone() three times, why doesn't the highest class in the inheritance heirachy, just under class Object, get a copy of that class returned?

Suppose we have three classes: A, B and C, where A -> B -> C (inherit = ->)

Then calling super.clone() in class C, invokes clone() in B which calls super.clone(), invoke clone() in A which call super.clone() 'this time Object.clone() gets called'. Why is it not a copy of the this object with respect to class A that gets returned from Object.clone()? That sounds logical to me.

5 Answers

this class has information about this class as well as it is associated with another class. So, conceptually this class's object will have information of associated class as well. this object is incomplete object without associated object/parent class.all direct as well as indirect fields in this class needs to be copied make it a worth it new clone of current object. we cannot access only that portion of reference which only denotes child section.

Related