I want to implement Serializable into a class, but I am not allowed to modify it. My idea was to make another class which inherits from the other class. But the original class is built like this:
public class ClassName {
private class InnerClass{
private InnerClass(){
//do stuff
}
}
public ClassName(){
//do stuff
}
}
So following does not work because InnerClass
is private thus not visible:
import java.io.Serializable;
public class ClassTwo extends ClassName implements Serializable{
private class InnerClass extends ClassName.InnerClass implements Serializable {
private InnerClass(){
super();
}
}
public ClassTwo(){
super();
}
}
Is there a way to implement Serializable into ClassName
without changing the original class?
If you have read/write access to all the slots you care to serialize, you can create an adapter class.
public class MyClassAdapter implements Serializable {
private MyClass instance;
...
}
Then just wrap the instance in this adapter class before serializing, and extract it after deserializing.
If you don't have read/write access to the slots, there's probably a good reason for that, and serializing data you don't own is a bad idea.