Can you please help me understand something in implementing the Serializable interface in a super class.
1. When the super class implements the Serializable, then do sub-classes need to explicitly declare that they are implementing the Serializable interface?
2. Where is the use of implementing Serializable important between:
2.1 - A Data Object (Data Transfer) class - which basically holds the state.
2.2 - A Helper class that has methods to say - query the DB and create instances of Data Objects to represent state.
3.3 - Both of the above.
3. Is it necessary to "declare a static final serialVersionUID field of type long" when implementing Serializable. If so, what role does this value play?
Regards, Siphiwe Madi
[SCJP, SCWCD, __] Next ... scjwsd
1). You do not need - implementing and extending are transitive - it's OO concept.
3). I think that that field is optional. It is for better performance - I guess. Someone else should agree with me, because I am not sure.
1. Well this thing is pretty clear using the inheritance tree. If the superclass implements serializable then the sub class will too through inheritance.
2. Serialization is usefull in 2.1. Well let me take it in terms of databases, we take dumps and transfer it, does it include the operations or does it include the values. Well serialization is basically came from restore.
3. As the point above it comes from restoration we cannot use any template for the restoration. So to specify that particular template the serialVersionUID is specified. Well its static final so it will be there in the template but not in the object. Well when the object is serialized, the object will be stamped with class's serialVersionUID. Then well you think the class was out dated and you try to update making some changes which probably changes the serialVersionUID. Then when you try to deserialize the object, surprise surprise the object won't deserialize. So now to make this happen we ourselves put the serialVersionUID in the updated class. That's about all of it.
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api