-1

Object class clone() method has native implementation which creates instance of child class and copies the state of source object to newly created instance.

Question:

  1. clone() method of object class doesn't invoke constructor of child class then how does it creates instance of the child class?
  • 1
    Does [this](https://howtodoinjava.com/java/cloning/a-guide-to-object-cloning-in-java/) answer your question? – Wouter van der Linde Apr 20 '21 at 11:03
  • nope. @WoutervanderLinde My question is simple. Child childObj = new Child(); Child childObj1 = childObj.clone(); childObj1 == childObj // false. it means new object is created. Now who creates this new Object and how it is created without invoking constructor is my question. I am not interested in understanding college level knowlede of clone in java –  Apr 20 '21 at 11:23
  • Does this answer your question? [Understanding Object.clone() in Java](https://stackoverflow.com/questions/16688482/understanding-object-clone-in-java) – Joe May 30 '21 at 11:39

2 Answers2

1

Clone is implemented within the JVM in an implementation dependant way. In OpenJDK, clone is implemented as jvm_clone in jvm.cpp from line 627. This allocates the memory for the object and copies the data from the object it was called on.

Creating an instance and calling constructors are separate operations at the JVM level so native implementations don't need to call any constructor after creating an instance. By using the lower level JVM methods in C++ it doesn't need to call the constructor.

fgb
  • 18,439
  • 2
  • 38
  • 52
-1

Your definition is false. The clone() method does not create an instance of the child class, instead, it creates an instance of the class where the clone() method was called. This instance containing all of its current values. For this reason, it does not call the child class constructor, however, it does call the constructor of its own class.

MorganS42
  • 656
  • 4
  • 21
  • During the time of writing that comment I had not marked it negative, however, now I have. The reason for this being the fact that you have not provided a source for your definition, I recommend you do that before expecting a useful answer. – MorganS42 Apr 20 '21 at 11:24
  • 1
    My answer was not meant to be negative in the slightest. I had assumed that you had found that definition off of some website, knowing that website may have allowed me to better understand the definition by using the context around it. As you don't want to give a source, I am left to assume that you have invented that definition yourself, and I am sorry to inform you that it is incorrect, or at the very least misworded. – MorganS42 Apr 20 '21 at 11:33
  • 2
    @DeepakAgrawal about "Let experts first read it and comment over it" please don't call into doubt the expertise of other users when you have no way of judging it. Your definition is understandable but imprecise ("child class" usually means "a class that inherits from another class", not "an instance of another class inside the class being cloned", which I guess is what you actually mean) so I can see how MorganS42 might find it confusing. That doesn't call their expertise into question. And there's nothing negative in any of their comments. – Federico klez Culloca Apr 20 '21 at 13:50