Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

cannot change a non serializable class which has no no-arg constructor  RSS feed

 
John Maak
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I know that this is quite an old problem and comes up all the time, but I could not really find the solution so far.
So, what can I do if I'd like to re-use a class which is not serializable but does not have a no-arg constructor and I need to make the subclass serializable?

E.g. the class I'd like to reuse and cannot change:
<pre>
public class Parent {
private final int age;

public Parent(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
</pre>
My subclass that I need to make serializable:
<pre>
import java.io.Serializable;
public class SerializableChild extends Parent implements Serializable {
private String nickName;

public SerializableChild(int age, String nickName) {
super(age);
this.nickName = nickName;
}

public String getNickName() {
return nickName;
}
}
</pre>

Serialization/deserialization does not work because the first non-serializable ancestor (Parent) does not have an available no-arg constructor.

Theoretically creating a SerializableChild instance with the same state should not be such a big problem because I can access all the necessary data (i.e. age and nickName) but because the age can be set only at instantiation I have difficulties with the implementation.

Does anyone know a nice solution to this problem?

The best I can think of is using the Memento Pattern and serialize the memento, but this is not the same.

Thanks for any help,
John
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!