Java – How to resolve compiler error – clone() has protected access in java.lang.Object

If you are trying to clone an object in java and are getting the below compilation error –

clone() has protected access in java.lang.Object

Then the reason why you are getting the above compiler error is that you have not followed either 1 or both of the basic required steps for cloning an object in Java, which are

  • STEP 1 – The class of the object being cloned should implement java.lang.Cloneable interface.
  • STEP 2 – The class should override clone() method of java.lang.Object class.

Once you implement the above 2 steps in the class of the object being cloned then the compiler error will stop appearing.

 

Digiprove sealCopyright © 2014-2021 JavaBrahman.com, all rights reserved.