This is raised because of the technical difficulties faced in my Project.
Problem: I need to clone a Object of a Class where it extended the properties(Inheritance) from a third party library class(where we don't have access to modify its contents)
Let me explain with example below:
Parent Class:
public class UnChangeableBaseClass {
//fields and Methods
}
Child Class:
class DerivedLocalClass extends UnChangeableBaseClass implements Cloneable {
// local fields and methods
public Object clone(){
Object clonedObj= null;
try{
clonedObj = super.clone();
}
catch(CloneNotSupportedException e){
//log exceptions
}
}
}
When I try to do this, super.clone()
method refers to Class - UnChangeableBaseClass
Type and it doesn't overrides the Object clone()
methods. I believe all classes were extended with java.lang.Object class
, implicitly protected Object clone()
method would be inherited to this Parent Class. So, i thought in such a way that this method in Derived Class would overrides the Parent/Object clone method. But during runtime JVM search for the clone method explicitly defined in UnChangeableBaseClass
. Hope I explained in proper way without confusing you.
My questions are follows:
How can I implement clone method in this typical case, where we cannot add any method
in parent class to havesuper.clone()
to call Object clone method.If above case is not possible, is there any other way to clone the Derived Class
Object (by considering all the limitations in above scenario)Finally, just to know the reason for this JVM behaviour (described above).
Correct Method signature is below
Clone is
Protected
method inObject
class so it is accessible to you inside class, and if you are extending from it.super.clone()
is just needed to callclone()
method from object which calls methodinternalClone
onthis
which is current class object.So above
clone()
method insideObject
will only throwCloneNotSupportedException
if instance on which it is called is notCloneable
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()
.So below code works fine
References :