I am confused regarding overriding clone method in the class for which I want cloned object.
Object class has protected object method and as per the protected behavior which is When a method is protected, it can only be accessed by the class itself, subclasses of the class, or classes in the same package as the class.
As every class in Java extends from Object, so it should have clone method but still we are forced to override clone. Why is it required?
Also, I have read at some places to override the clone object and make it public. I wonder, why is it so?
All answers are welcome.
No you are not forced to override the
clone
method. In inheritance, when you inherit a class, you are not forced to override it's method. Its modifier being public or protected doesn't make much of a difference. However, if you want to invoke a method directly onsuper
class reference, then that method has to bepublic
. Protected methods are accessible only through inheritance. That is you can only access them throughsubclass
reference. Or if you override the method, you can access them throughsuper
keyword.Having said that, you should not override
clone
method, as it isbroken
. Because, for a class to be cloned, you need to implement theCloneable
interface. And then your class uses theclone
method ofObject
class instead. Because,Cloneable
interface doesn't exactly have any method forcloning
. It would be a better option to useCopy Constructor
instead.For more details, I would suggest to go through this chapter of
Joshua Bloch's
Effective Java, which covers all aspects of usingclone
method.Effective Java- Item # 11 - Override clone judiciously
I'd recommend reading Joshua Bloch's Effective Java 2nd edition. It has a good chapter discussing clone.
I would not advise doing this. I consider this a JDK 1.0 mistake. The book will make that clearer.
I'd recommend writing a copy constructor instead to get what you want:
In many cases it's not clear what a cloned object should be and how it should behave, so if you want your class to be clonable you have to say so explicitly by overriding clone and making it public.
Cases where clone might not make sense include classes that represent some resource, like a network connection or a synchronization lock. If these objects could be cloned it's not clear how the clone should behave. For example, does the clone of a network connection have a TCP/IP connection of its own or does it somehow use the existing one?
Clone is
Protected
method inObject
class so it is accessible to you inside class.About access- When a method is protected, it can only be accessed by the class itself, subclasses of the class, or classes in the same package as the class.
I see some misconceptions about clone method
clone()
method isprotected
insideObject
class so you can not callclone()
outside of class. e.g.child.clone()
unless you override it and make accesspublic
Cloneable
is marker interface and if you do not mark classCloneable
then you will getCloneNotSupportedException
if you callclone()
methodsuper.clone
need to be modified.super.clone
. If a class and all of itssuperclasses (except Object)
obey this convention, it will be the case thatx.clone().getClass() == x.getClass()
.Method signature is below
References :