writeObject signature

Discussion in 'Java' started by Roedy Green, Jun 8, 2004.

  1. Roedy Green

    Roedy Green Guest

    I just noticed that it is writeObject ( Object o )
    rather than writeObject ( Serializable o )

    how come?

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 8, 2004
    #1
    1. Advertising

  2. Roedy Green

    Skippy Guest

    > I just noticed that it is writeObject ( Object o )
    > rather than writeObject ( Serializable o )


    I wondered the same thing, same for readObject.
    Seems to me this is a design flaw.

    Would have been nice to have this compile-time 'check',
    before you launch the app and Exceptions are thrown..
    Skippy, Jun 8, 2004
    #2
    1. Advertising

  3. Roedy Green

    Roedy Green Guest

    On Tue, 8 Jun 2004 15:22:14 +0200, "Skippy" <> wrote or
    quoted :

    >I wondered the same thing, same for readObject.
    >Seems to me this is a design flaw.


    My best explanation is that you are going to do a cast check anyway on
    reconstitution, and so the cast check to Serializable it would have to
    do internally would be wasted.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 8, 2004
    #3
  4. Roedy Green

    Roedy Green Guest

    On Tue, 08 Jun 2004 14:14:14 GMT, Roedy Green
    <> wrote or quoted :

    >My best explanation is that you are going to do a cast check anyway on
    >reconstitution, and so the cast check to Serializable it would have to
    >do internally would be wasted.


    Then why not put one on writeObject? To be parallel? To make the
    name of the method logical?

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 8, 2004
    #4
  5. Roedy Green

    Liz Guest

    "Roedy Green" <> wrote in message
    news:...
    > I just noticed that it is writeObject ( Object o )
    > rather than writeObject ( Serializable o )
    >
    > how come?
    >
    > --
    > Canadian Mind Products, Roedy Green.
    > Coaching, problem solving, economical contract programming.
    > See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


    It's obvious.
    Liz, Jun 8, 2004
    #5
  6. Roedy Green

    Roedy Green Guest

    On Tue, 08 Jun 2004 15:21:20 GMT, "Liz" <> wrote or
    quoted :

    >news:...
    >> I just noticed that it is writeObject ( Object o )
    >> rather than writeObject ( Serializable o )
    >>
    >> how come?
    >>
    >> --
    >> Canadian Mind Products, Roedy Green.
    >> Coaching, problem solving, economical contract programming.
    >> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

    >
    >It's obvious.


    That reminds me of a story Professor Melzak told. A math prof was
    scribbling out a proof on the board. A student raised his hand, "how
    did you get from step A to step B?". "It's trivial the prof replied".
    "I don't see it" the student countered.


    The prof stopped, and scratched his head. Eventually he left the room
    to go think it over, and just before the bell, returned to the class,
    and announced triumphantly "It IS trivial".


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 8, 2004
    #6
  7. Roedy Green

    lyallex Guest

    On Tue, 08 Jun 2004 15:21:20 GMT, "Liz" <> wrote:

    >
    >"Roedy Green" <> wrote in message
    >news:...
    >> I just noticed that it is writeObject ( Object o )
    >> rather than writeObject ( Serializable o )
    >>
    >> how come?
    >>
    >> --
    >> Canadian Mind Products, Roedy Green.
    >> Coaching, problem solving, economical contract programming.
    >> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

    >
    >It's obvious.
    >


    Ah, great, can you tell us then.
    I have just asked the most experienced java developer/architect I know
    and he didn't know the answer ...

    Rgds
    Lyall



    "Process- How will the work and the team be organized?
    The team needs to fit the culture in which it will operate,
    but you should write software well rather than preserve the
    irrationality of an enclosing culture" - Kent Beck
    lyallex, Jun 9, 2004
    #7
  8. Roedy Green schrieb:
    > I just noticed that it is writeObject ( Object o )
    > rather than writeObject ( Serializable o )
    >
    > how come?
    >


    IMO: I'd say that this is because writeObject writes Objects. In it's
    default implementation it writes only objects that have implemented
    Serializable. But one may want to provide it's own method to write
    Objects, not relying on Serializable.

    Bye
    Michael
    Michael Rauscher, Jun 9, 2004
    #8
  9. Roedy Green

    lyallex Guest

    On Wed, 09 Jun 2004 12:44:40 +0200, Michael Rauscher
    <> wrote:

    >Roedy Green schrieb:
    >> I just noticed that it is writeObject ( Object o )
    >> rather than writeObject ( Serializable o )
    >>
    >> how come?
    >>

    >
    >IMO: I'd say that this is because writeObject writes Objects. In it's
    >default implementation it writes only objects that have implemented
    >Serializable. But one may want to provide it's own method to write
    >Objects, not relying on Serializable.
    >
    >Bye
    >Michael


    I thought this might be the answer until I realised that writeObject
    is final and therefore can't be overridden ... there is a method
    called writeObjectOverride(Object obj) that the API docs say should be
    used to 'override' the default writeObject method (?) but this just
    seems to reinforce the original notion that writeObject should take an
    argument of type Serializable.

    Do you think it could be a 'style' issue.

    In fact we have just had a Development consultant here from Sun
    Microsystems and even he couldn't come up with good answer.

    Rgds
    Lyall
    >
    >




    "Process- How will the work and the team be organized?
    The team needs to fit the culture in which it will operate,
    but you should write software well rather than preserve the
    irrationality of an enclosing culture" - Kent Beck
    lyallex, Jun 9, 2004
    #9
  10. Roedy Green

    Carl Howells Guest

    Roedy Green wrote:
    > I just noticed that it is writeObject ( Object o )
    > rather than writeObject ( Serializable o )
    >
    > how come?
    >


    Because it can also write Externalizable objects. And because the
    behavior of Externalizable and Serializable are different enough,
    neither one is a superinterface of the other.

    Of course, since the behavior of writeObject has to be significantly
    different between the two interfaces, this makes one wonder why they
    didn't just put two different writeObject methods in, on taking a
    Serializable instance, the other taking an Externalizable instance.

    I suppose the answer to that is also pretty obvious... If you have an
    object that you can send through an ObjectOutputStream, and you don't
    know if it's Serializable or Externalizable, someone has to do
    instanceof and casts, no matter what. Might as well make them part of
    the ObjectOutputStream, instead of forcing the user to write them over
    and over again.
    Carl Howells, Jun 9, 2004
    #10
  11. lyallex schrieb:
    > On Wed, 09 Jun 2004 12:44:40 +0200, Michael Rauscher
    > <> wrote:
    >
    >
    >>Roedy Green schrieb:
    >>
    >>>I just noticed that it is writeObject ( Object o )
    >>>rather than writeObject ( Serializable o )
    >>>
    >>>how come?
    >>>

    >>
    >>IMO: I'd say that this is because writeObject writes Objects. In it's
    >>default implementation it writes only objects that have implemented
    >>Serializable. But one may want to provide it's own method to write
    >>Objects, not relying on Serializable.
    >>
    >>Bye
    >>Michael

    >
    >
    > I thought this might be the answer until I realised that writeObject
    > is final and therefore can't be overridden ... there is a method


    In fact :-(

    > called writeObjectOverride(Object obj) that the API docs say should be
    > used to 'override' the default writeObject method (?) but this just
    > seems to reinforce the original notion that writeObject should take an
    > argument of type Serializable.


    Ok, then let's assume that overriding isn't the argument. Even so, I
    don't think that writeObject should take a Serializable. I think that
    this has something to do with design.

    If writeObject would take an argument of type Serializable, this would
    only work because AFAIK all objects in (current) Java are instances of
    Object.

    Serializable doesn't declare any methods. If we would rely only on the
    Serializable interface we wouldn't be able to use any method. E.g. there
    would be no way to call getClass (because Serializable doesn't define it).

    IOW: giving an Object instance to writeObject makes it obvious that
    writeObject may send Object-messages to the instance whereas using
    Serializable does not.

    All IMO of course.

    Bye
    Michael
    Michael Rauscher, Jun 9, 2004
    #11
  12. Carl Howells schrieb:
    > Roedy Green wrote:
    >
    >> I just noticed that it is writeObject ( Object o )
    >> rather than writeObject ( Serializable o )
    >>
    >> how come?
    >>

    >
    > Because it can also write Externalizable objects. And because the
    > behavior of Externalizable and Serializable are different enough,
    > neither one is a superinterface of the other.


    Externalizable extends Serializable.

    Bye
    Michael
    Michael Rauscher, Jun 9, 2004
    #12
  13. Roedy Green

    Carl Howells Guest

    Michael Rauscher wrote:
    >
    > Externalizable extends Serializable.
    >


    Mm... That'll teach me to post without actually checking at least once
    to see if what I'm thinking has any relationship with reality... :/
    Carl Howells, Jun 9, 2004
    #13
  14. Michael Rauscher wrote:
    >
    > Serializable doesn't declare any methods. If we would rely only on the
    > Serializable interface we wouldn't be able to use any method. E.g. there
    > would be no way to call getClass (because Serializable doesn't define it).


    Actually, a reference whose type is an interface can utilize the methods declared for
    Object. For example,

    Serializable thing = new Integer(1);
    Class thingClass = thing.getClass();

    .... will work just fine.

    --
    Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
    ==============================================================
    * The Ultimate DBMS is here!
    * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
    Lee Fesperman, Jun 9, 2004
    #14
  15. Roedy Green

    Roedy Green Guest

    On Wed, 09 Jun 2004 12:44:40 +0200, Michael Rauscher
    <> wrote or quoted :

    >IMO: I'd say that this is because writeObject writes Objects. In it's
    >default implementation it writes only objects that have implemented
    >Serializable. But one may want to provide it's own method to write
    >Objects, not relying on Serializable.


    But even if you did use your own technique, would you not still use
    the Serialisable marker? You need that in case somebody writes an
    object that points to your object.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 9, 2004
    #15
  16. Roedy Green

    Roedy Green Guest

    On Wed, 09 Jun 2004 20:04:47 +0200, Michael Rauscher
    <> wrote or quoted :

    >Ok, then let's assume that overriding isn't the argument. Even so, I
    >don't think that writeObject should take a Serializable. I think that
    >this has something to do with design.
    >
    >If writeObject would take an argument of type Serializable, this would
    >only work because AFAIK all objects in (current) Java are instances of
    >Object.


    that begins to make sense.
    However I found I could do this:

    Serializable s = ...

    fos.writeObject( s );

    So serialisables are considered also objects.

    Inside writeObject may be another matter.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 9, 2004
    #16
  17. Roedy Green

    Liz Guest

    "lyallex" <> wrote in message
    news:...
    > On Tue, 08 Jun 2004 15:21:20 GMT, "Liz" <> wrote:
    >
    > >
    > >"Roedy Green" <> wrote in message
    > >news:...
    > >> I just noticed that it is writeObject ( Object o )
    > >> rather than writeObject ( Serializable o )
    > >>
    > >> how come?
    > >>
    > >> --
    > >> Canadian Mind Products, Roedy Green.
    > >> Coaching, problem solving, economical contract programming.
    > >> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

    > >
    > >It's obvious.
    > >

    >
    > Ah, great, can you tell us then.
    > I have just asked the most experienced java developer/architect I know
    > and he didn't know the answer ...
    >

    The most experienced java developer/architect I know is my dog.
    Liz, Jun 9, 2004
    #17
  18. Lee Fesperman schrieb:
    > Michael Rauscher wrote:
    >
    >>Serializable doesn't declare any methods. If we would rely only on the
    >>Serializable interface we wouldn't be able to use any method. E.g. there
    >>would be no way to call getClass (because Serializable doesn't define it).

    >
    >
    > Actually, a reference whose type is an interface can utilize the methods declared for
    > Object. For example,
    >
    > Serializable thing = new Integer(1);
    > Class thingClass = thing.getClass();
    >
    > ... will work just fine.


    Yes. It works with current versions of the *Java* language because every
    object is an instance of Object. Therefore you can cast any object to
    type Object and only therefore you can call getClass. Don't think in
    Java, draw e.g. an UML static structure diagramm and you'll see, what I
    mean.

    Bye
    Michael
    Michael Rauscher, Jun 9, 2004
    #18
  19. Roedy Green schrieb:
    > On Wed, 09 Jun 2004 20:04:47 +0200, Michael Rauscher
    > <> wrote or quoted :
    >
    >
    >>Ok, then let's assume that overriding isn't the argument. Even so, I
    >>don't think that writeObject should take a Serializable. I think that
    >>this has something to do with design.
    >>
    >>If writeObject would take an argument of type Serializable, this would
    >>only work because AFAIK all objects in (current) Java are instances of
    >>Object.

    >
    >
    > that begins to make sense.
    > However I found I could do this:
    >
    > Serializable s = ...
    >
    > fos.writeObject( s );
    >
    > So serialisables are considered also objects.


    Due to Javas architecture, anything (AFAIK) is an instance of Object.
    Therefore you can cast anything to Object.

    Bye
    Michael
    Michael Rauscher, Jun 10, 2004
    #19
  20. Michael Rauscher schrieb:
    > Roedy Green schrieb:
    >
    >> On Wed, 09 Jun 2004 20:04:47 +0200, Michael Rauscher
    >> <> wrote or quoted :
    >>
    >>
    >>> Ok, then let's assume that overriding isn't the argument. Even so, I
    >>> don't think that writeObject should take a Serializable. I think that
    >>> this has something to do with design.
    >>>
    >>> If writeObject would take an argument of type Serializable, this
    >>> would only work because AFAIK all objects in (current) Java are
    >>> instances of Object.

    >>
    >>
    >>
    >> that begins to make sense.
    >> However I found I could do this:
    >>
    >> Serializable s = ...
    >>
    >> fos.writeObject( s );
    >>
    >> So serialisables are considered also objects.

    >
    >
    > Due to Javas architecture, anything (AFAIK) is an instance of Object.
    > Therefore you can cast anything to Object.


    An addition:

    For example there's nothing that prevents the Java developers from
    creating a new root of the class hierarchy, let's say an AbstractObject
    superclass of Object and from introducing a new class tree starting at
    AnotherObject which is a subclass of AbstractObject (this is just a
    stupid example)

    Now, one could easily write a

    class X extends AnotherObect implements Serializable {
    //...
    }

    Instances of X aren't instances of Object anymore. Now if writeObject
    would rely on the serializable interface, the cast to Object would e.g.
    result in a ClassCastException.

    >
    > Bye
    > Michael
    >
    Michael Rauscher, Jun 10, 2004
    #20
    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. JJBW
    Replies:
    1
    Views:
    10,061
    Joerg Jooss
    Apr 24, 2004
  2. Kaidi
    Replies:
    4
    Views:
    2,339
    Kaidi
    Jan 3, 2004
  3. Kaidi
    Replies:
    7
    Views:
    4,354
    Robert Olofsson
    Jan 6, 2004
  4. JMC
    Replies:
    1
    Views:
    770
    Roedy Green
    Apr 7, 2004
  5. Gustavo Narea
    Replies:
    14
    Views:
    850
    Gustavo Narea
    Feb 16, 2009
Loading...

Share This Page