I'm not sure if I understand how serializable in java works?

Only serialize the things that hold data you need to get back. In this case, A seems to be the likely candidate.

Only serialize the things that hold data you need to get back. In this case, A seems to be the likely candidate. I wouldn't serialize D since it is simply an aggregate of persisted objects... I would allow a method that lets me retrieve all instances of A and its children (getData()) in an array or something that lets me store everything.

Then another method that lets me rebuild D with an array of A. Also since E doesn't hold any data, it does not need to be serialize (does not have any runtime data that needs to be persisted). B and C will inherit serializable from A.

Just ask yourself, if I restarted my app, what data I do want to have when I turn it back on. Try to make your serialized objects as lightweight as possible, and you'll make your life easier.

In general, you should serialize objects that you need to get back to your initial state after a load/save operation. HOWEVER! Say you have a really complex application.

Say you only NEED B and C. Say you use D only for calculation. So you can retrieve the state of D only knowing B and C.

But what if you have some state-of-the-art dynamic algorithm in D. Wouldn't you rather save it than go through all that computation again? Think about the fact that the logic taking place in D can be really really costly.So it might take very long to replicate, but relatively easy to save and retrieve.

My answer is: adapt to your specific needs. There's no always good/always bad way. I'm not saying you should or shouldn't serialize D in your case, because I don't know a lot of details.

Just that you shouldn't take answers like do it/don't do it lightly.

The idea of serialization is that you want to re-create instance later or on different machine. Maybe it helps to think about what do we need to re-create, what can be created locally. Abstract class A implements Serializable { private int a; } abstract class B extends A { private int b; B(int b) { this.

B=b;} } abstract class C extends A { private int c; C(int c) { this. C=c;} } This allows to serialize/deserialize instances of B and C. Class D implements Serializable { B someB; C someC; D(int b, int c) { someB = new B(b); someC = new C(c); } } In this case D has to serializable because we want a D instance with exactly the same B and C objects.

If E just provides methods, then we might don't need to serialize it - unless D references an instance of E and we need it to re-create a valid D instance.

Every object which you intend to serialize should implement Serializable interface. In your the class A and D needs to implement it. You need not serialize E as it does not have any state.In generally all the classes which hold your state (has any instance level variable roughly speaking) should directly or indirectly implement Serializable if they are to be Serialized.

– Santosh Sep 19 at 13:15 My reason is that I don't agree with you in that you shouldn't serialize E (I guess you mean shouldn't, otherwise this wouldn't really be a constructive answer... you also need not serialize C or D, but you do)... Anyway, check my answer. – Luchian Grigore Sep 19 at 13:19 @ Luchian, but E does not have any state data (also pointed out byDaryl Teo) why would you want to serialize it? Its utility class as mentioned by OP.

– Santosh Sep 19 at 13:34 The op doesn't specify much. Even an utility class can have important members which you don't 'need' to serialize but might want to do... – Luchian Grigore Sep 19 at 13:43 I agree. But in that case you can put a comment against the question asking for more details.

My answer was in general. – Santosh Sep 19 at 13:46.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions