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 have super.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
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
Clone is Protected
method in Object
class so it is accessible to you inside class, and if you are extending from it.
super.clone()
is just needed to call clone()
method from object which calls method internalClone
on this
which is current class object.
internalClone((Cloneable) this);
So above clone()
method inside Object
will only throw CloneNotSupportedException
if instance on which it is called is not Cloneable
I see some misconceptions about clone method
clone()
method is protected
inside Object
class so you can not call clone()
outside of class. e.g. child.clone()
unless you override it and make access public
Cloneable
is marker interface and if you do not mark class Cloneable
then you will get CloneNotSupportedException
if you call clone()
methodsuper.clone
need to be modified. super.clone
. If a class and all of its superclasses (except Object)
obey this convention, it will be the case that x.clone().getClass() == x.getClass()
. So below code works fine
public class Child extends UnChangeableBaseClass
implements
Cloneable {
int index = 0;
public Child(int index) {
this.index = 10;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
References :