1

To clone an object of a class we implement the Cloneable interface, and override the clone method:

protected Object clone() throws CloneNotSupportedException{
        return super.clone();
    }

The above super.clone() calls Object's native API clone().

In case of Serializable the instanceof check is done in ObjectOutputStream class. Similarly, I am trying to find out where does the compiler check whether the object is an instanceof Cloneable or not? Is the check performed natively?

ambar
  • 2,053
  • 6
  • 27
  • 32

1 Answers1

0

Why should compiler check this if it's already a clone implementation on Object class. The exception is relate to other implementations that require Cloneable to be implemented to provide other services. Actually serialization requires a Serializable implementation. The user then can choose/adjust the Cloneable implementation that is required.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • If a class doesn't implement the Cloneable interface, overrides the clone method, and an object of it calls clone. In that case the call goes to native clone(), and most probably there is the check for 'obj instanceof Clonebale', which throws the CloneNotSupportedException. – ambar Jun 08 '14 at 12:26
  • No, `obj instanceof Clonebale` is made to make sure an object has `Cloneable` interface impl. You can omit `CloneNotSupportedException` if you catch it by the method. – Roman C Jun 08 '14 at 12:30