1

1.Child class extends Parent class. 2.Child class implements Cloneable and overrides clone() method calls super.clone() 3.Parent class doesn't implement Cloneable interface neither it overrides clone() method.

Output: Both parent and child class state's are cloned.

Question: How Parent class state is cloned by Object class when Parent class has not implemented marker interface Cloneable?

class ParentCloneableClass{
    String val;
    public ParentCloneableClass(String val){
        this.val = val;
    }

    public String getVal() {
        return val;
    }
}
class CloneableClass extends ParentCloneableClass implements Cloneable{
    String name;
    public CloneableClass(String name){
        super("parentClass");
        this.name = name;
    }

    @Override
    public   CloneableClass clone() throws CloneNotSupportedException {
        return (CloneableClass) super.clone();
    }

    public String getName() {
        return name;
    }
}

class Demo{
   public static void main(String[] args) throws CloneNotSupportedException {
        CloneableClass cloneableClass = new CloneableClass("deepak");
        CloneableClass cloneableClassCloned = cloneableClass.clone();
   }
}
  • `Parent.clone()` delegates to `Object.clone()` *implicitly* – Lino Apr 20 '21 at 10:55
  • Parent doesn't override clone(). So, how does parent.clone() invoke object.clone()? If a class doesn't override clone() and if we invoke child.clone() then gets compile time error then how come parent has not override clone() will give a call to parent clone(). –  Apr 20 '21 at 10:58
  • @DeepakAgrawal: Object has the `clone()` method which parent inherits (which all classes inherit). It doesn't need to override anything to have the method. – Hovercraft Full Of Eels Apr 20 '21 at 11:01
  • @DeepakAgrawal see this example: https://ideone.com/nF4irN. It behaves the same with `clone()` – Lino Apr 20 '21 at 11:04

1 Answers1

1

The class Object checks the runtime type of the object being cloned, not the compile time type. So the object at runtime is of type Cloneable which does implements Cloneable, hence the Object class performs the cloning accordingly:

The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown... Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.

M A
  • 71,713
  • 13
  • 134
  • 174
  • Object invocation occurs runtime that's fine, so here the instance of object at runtime is of child, how come object class clone Parent class automatically? –  Apr 20 '21 at 11:02
  • @DeepakAgrawal It will copy all fields of the child class, including those inherited from parent. – M A Apr 20 '21 at 11:04