Serialization: readObject() and self method calls?

J

Jim Garrison

When is it acceptable for readObject and writeObject to call other
public/protected object methods?

The following code is from a version of GregorianCalendar in IBM's
WebSphere Application Developer product. The 'override' of readObject
was introduced in the latest fixpack for WSAD, and breaks some of my
code.

private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException { /*ibm@56174*/
stream.defaultReadObject(); /*ibm@56174*/
setGregorianChange(new Date(gregorianCutover)); /*ibm@56174*/
} /*ibm@56174*/

I had created a subclass of GregorianCalendar to represent immutable
dates, and had overridden setGregorianChange to throw an
UnsupportedOperationException. Needless to say, after this update any
attempt to deserialize an instance of my subclass fails.

My intuitive feeling is that serialization and deserialization are
supposed to happen "under the covers", and should not involve the
object's public or protected interfaces. The reason for this is that
public and protected methods can be overridden by subclasses, and
therefore their behavior is not guaranteed to be whatever the base
class thinks it is. This can lead to fatal errors, as it did in this
case.

I would guess that readObject and writeObject should limit themselves
to either direct internal manipulation or use of private interfaces
only.

I'd really like some comments from more-knowledgeable Java folks on
this one.

TIA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top