Serialization: readObject() and self method calls?

Discussion in 'Java' started by Jim Garrison, Oct 21, 2003.

  1. Jim Garrison

    Jim Garrison Guest

    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
    Jim Garrison, Oct 21, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mitch gart
    Replies:
    1
    Views:
    367
    David Zimmerman
    Oct 18, 2003
  2. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    572
    Lonnie Princehouse
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    591
    Bengt Richter
    Jul 11, 2005
  4. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    396
    Dan Sommers
    Jul 12, 2005
  5. falcon
    Replies:
    0
    Views:
    371
    falcon
    Jul 31, 2005
Loading...

Share This Page