Overriding existing Serializeable writeObject ?

Discussion in 'Java' started by jprogrammer, Apr 20, 2004.

  1. jprogrammer

    jprogrammer Guest

    I have a rendering class that extends AWT Panel for drawing purposes. I
    made this class Serializable so that I can send it over the socket. The
    problem I found out is that my implementation of writeObject () and
    readObject() for my rendering frame is completely ignored!! Panel class's
    default writeObject, readObject is invoked instead. I tested this using
    println(). How do I fix this? Here's some code out of my class:

    public class DrawFrame extends Panel implements Serializable,MouseListener,
    MouseMotionListener{


    public int side1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    public int side2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    public int side3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    public int side4[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    public int side5[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
    public int side6[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};

    //------------------
    private void writeObject(ObjectOutputStream out)
    {
    try{
    for(int i=0;i<9;i++)
    {
    out.writeInt(side1);
    out.writeInt(side2);
    out.writeInt(side3);
    out.writeInt(side4);
    out.writeInt(side5);
    out.writeInt(side6);
    }

    }
    catch(IOException e)
    {
    //System.out.println(e);
    }

    }
    private void readObject(ObjectInputStream in)
    {
    try{
    for(int i=0;i<9;i++)
    {
    side1=in.readInt();
    side2=in.readInt();
    side3=in.readInt();
    side4=in.readInt();
    side5=in.readInt();
    side6=in.readInt();
    }
    }
    //catch(ClassNotFoundException e){}
    catch(IOException e){}

    }
    jprogrammer, Apr 20, 2004
    #1
    1. Advertising

  2. On Tue, 20 Apr 2004 09:37:10 -0400, jprogrammer wrote:

    > I have a rendering class that extends AWT Panel for drawing purposes. I
    > made this class Serializable so that I can send it over the socket. The
    > problem I found out is that my implementation of writeObject () and
    > readObject() for my rendering frame is completely ignored!! Panel class's
    > default writeObject, readObject is invoked instead. I tested this using
    > println(). How do I fix this? Here's some code out of my class:


    [...]

    > private void writeObject(ObjectOutputStream out)


    [...]
    > private void readObject(ObjectInputStream in)


    You didn't read the API or didn't understand it:

    Classes that require special handling during the
    serialization and deserialization process must
    implement special methods with these exact signatures:

    private void writeObject(java.io_ObjectOutputStream out)
    throws IOException
    private void readObject(java.io_ObjectInputStream in)
    throws IOException, ClassNotFoundException;

    Your signatures miss the throws-parts.


    Regards, Lothar
    --
    Lothar Kimmeringer E-Mail:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
    Lothar Kimmeringer, Apr 20, 2004
    #2
    1. Advertising

  3. jprogrammer

    Sudsy Guest

    jprogrammer wrote:
    > I have a rendering class that extends AWT Panel for drawing purposes. I
    > made this class Serializable so that I can send it over the socket. The
    > problem I found out is that my implementation of writeObject () and
    > readObject() for my rendering frame is completely ignored!! Panel class's
    > default writeObject, readObject is invoked instead. I tested this using
    > println(). How do I fix this?

    <snip>

    The javadocs specify:
    "Classes that require special handling during the serialization and
    deserialization process must implement special methods with these exact
    signatures:

    private void writeObject(java.io_ObjectOutputStream out)
    throws IOException
    private void readObject(java.io_ObjectInputStream in)
    throws IOException, ClassNotFoundException;"

    Your classes don't throw the required exceptions and so don't match
    the signatures. The javadocs should always be your first point of
    reference...
    Sudsy, Apr 20, 2004
    #3
  4. jprogrammer

    Roedy Green Guest

    On Tue, 20 Apr 2004 09:37:10 -0400, "jprogrammer" <> wrote
    or quoted :

    > The
    >problem I found out is that my implementation of writeObject () and
    >readObject() for my rendering frame is completely ignored!!


    see http://mindprod.com/jgloss/serialization.

    Did you leave out an "implements" anywhere?

    Do your method signatures exactly match?


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

    Paul Guest

    Oh I thought the methods "threw" the exceptions and i was supposed to catch
    them. I guess i completely misunderstood. Now, do i have to throw
    exceptions in any particular order?

    "Lothar Kimmeringer" <> wrote in message
    news:...
    > On Tue, 20 Apr 2004 09:37:10 -0400, jprogrammer wrote:
    >
    > > I have a rendering class that extends AWT Panel for drawing purposes. I
    > > made this class Serializable so that I can send it over the socket. The
    > > problem I found out is that my implementation of writeObject () and
    > > readObject() for my rendering frame is completely ignored!! Panel

    class's
    > > default writeObject, readObject is invoked instead. I tested this using
    > > println(). How do I fix this? Here's some code out of my class:

    >
    > [...]
    >
    > > private void writeObject(ObjectOutputStream out)

    >
    > [...]
    > > private void readObject(ObjectInputStream in)

    >
    > You didn't read the API or didn't understand it:
    >
    > Classes that require special handling during the
    > serialization and deserialization process must
    > implement special methods with these exact signatures:
    >
    > private void writeObject(java.io_ObjectOutputStream out)
    > throws IOException
    > private void readObject(java.io_ObjectInputStream in)
    > throws IOException, ClassNotFoundException;
    >
    > Your signatures miss the throws-parts.
    >
    >
    > Regards, Lothar
    > --
    > Lothar Kimmeringer E-Mail:
    > PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
    >
    > Always remember: The answer is forty-two, there can only be wrong
    > questions!
    Paul, Apr 21, 2004
    #5
  6. jprogrammer

    Roedy Green Guest

    On Tue, 20 Apr 2004 22:55:39 -0400, "Paul" <> wrote
    or quoted :

    >Oh I thought the methods "threw" the exceptions and i was supposed to catch
    >them. I guess i completely misunderstood. Now, do i have to throw
    >exceptions in any particular order?


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

    Tony Morris Guest

    > Your classes don't throw the required exceptions and so don't match
    > the signatures. The javadocs should always be your first point of
    > reference...
    >



    A method signature is strictly defined by its name and (order and type) of
    its parameters.
    The declared exceptions are NOT part of the method signature.
    Another misconception is that the return type of a method is part of its
    signature, which is incorrect.

    // same signature
    void m(int i, String s)
    int m(int i, String x) throws java.io.IOException
    String m(int x, String t) throws RuntimeException

    // different signature to those above
    void m(String s, int i) throws java.io.Exception
    void m(int i)
    void mm(int i, String s)

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
    Tony Morris, Apr 21, 2004
    #7
  8. On Tue, 20 Apr 2004 22:55:39 -0400, Paul wrote:

    > Oh I thought the methods "threw" the exceptions and i was supposed to catch
    > them. I guess i completely misunderstood. Now, do i have to throw
    > exceptions in any particular order?


    No, just put away the try-catch-blocks and add the throws-
    Statement in the signature of your method. You should catch
    the exceptions where you _use_ the serialization of the
    object.


    Regards, Lothar
    --
    Lothar Kimmeringer E-Mail:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
    Lothar Kimmeringer, Apr 22, 2004
    #8
  9. On Wed, 21 Apr 2004 16:56:52 +1000, Tony Morris wrote:

    >> Your classes don't throw the required exceptions and so don't match
    >> the signatures. The javadocs should always be your first point of
    >> reference...
    >>

    >
    > A method signature is strictly defined by its name and (order and type) of
    > its parameters.


    In this case it was meant that way, that
    you have to declare the method exactly as being described
    in the Javadocs

    > The declared exceptions are NOT part of the method signature.
    > Another misconception is that the return type of a method is part of its
    > signature, which is incorrect.


    I can try that later, but AFAIR the compiler don't accept
    methods with the same name and parameters but different
    return-types and you get compile-errors as well when you
    leave away or add exceptions to methods that are declared
    in superclasses.

    Google tells me I'm right and Patrick Niemeyer writes the
    same in his book "Exploring Java":

    A method signature is a collection of information about
    the method, as in a C prototype or a forward function
    declaration in other languages. It includes the method's
    name, type, and visibility, as well as its arguments
    and return type.


    Regards, Lothar
    --
    Lothar Kimmeringer E-Mail:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
    Lothar Kimmeringer, Apr 22, 2004
    #9
  10. On Thu, 22 Apr 2004 07:50:51 +0200, Lothar Kimmeringer wrote:

    > On Wed, 21 Apr 2004 16:56:52 +1000, Tony Morris wrote:

    ....
    >> A method signature is strictly defined by its name and (order and type) of
    >> its parameters.

    ....
    >> The declared exceptions are NOT part of the method signature.


    I was skeptical so I went on a long hunt
    at Sun to try and nail it down.. I saw one
    reference that suggested Tony was right,
    but it did not seem definitive to me.

    ....
    > I can try that later, but AFAIR the compiler don't accept
    > methods with the same name and parameters but different
    > return-types and you get compile-errors as well when you
    > leave away or add exceptions to methods that are declared
    > in superclasses.


    Then I thought to toss that and try it..
    <sscce>
    import java.io.IOException;

    public interface ImplementThis {
    public abstract String getTheStuff() throws IOException;
    }

    class Implementor implements ImplementThis {
    public String getTheStuff() {
    return "";
    }
    }
    </sscce>

    Compiles just fine..

    So, no - I now have to concede I have
    no idea why your code is failing, maybe
    we need to go to help level 2..
    <http://www.physci.org/codes/sscce.jsp>

    And note that URL does not refer to
    'some code' from your class, but a self
    contained example that fails.

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Apr 22, 2004
    #10
  11. jprogrammer

    Tony Morris Guest

    You'll find that what I mentioned is also in parallel with what you said.
    A method signature is defined exactly as I described.
    There has been no contradiction to that in your statements above.

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
    Tony Morris, Apr 22, 2004
    #11
    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. Kaidi
    Replies:
    4
    Views:
    2,339
    Kaidi
    Jan 3, 2004
  2. Kaidi
    Replies:
    7
    Views:
    4,356
    Robert Olofsson
    Jan 6, 2004
  3. JMC
    Replies:
    1
    Views:
    770
    Roedy Green
    Apr 7, 2004
  4. Roedy Green

    writeObject signature

    Roedy Green, Jun 8, 2004, in forum: Java
    Replies:
    35
    Views:
    1,264
    Dale King
    Apr 15, 2006
  5. juicy
    Replies:
    28
    Views:
    2,239
    Andrew Thompson
    Sep 25, 2004
Loading...

Share This Page