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).