Archived

This topic is now archived and is closed to further replies.

MOVSW

[java] Overriding default Serializable

Recommended Posts

THis is a LITE version of the problem as i don''t want to burden readers with non essential code. I have serialized a class that extends a AWT Panel class. The problem is that Panel class''s Serialization/Externalization overrides my own! How can i override the Panel''s default Serialization?? This particular case uses Externalization but i get same results as Serialization.
import java.io.*;
import java.awt.*;
public class ExTest extends Panel implements Externalizable
{
    int score;
    String cname;
    
    public ExTest(String n,int s)
    {
        cname=n;
        score=s;
    }
    public void readExternal(ObjectInput in)
    {
        try
        {
            score=in.readInt();
            cname=in.readUTF();
        }catch(IOException e)
        {
            System.out.println(e);
        }
    }
    public void writeExternal(ObjectOutput out)
    {
        try
        {
            out.writeInt(score);
            out.writeUTF(cname);
        }catch(IOException e)
        {
            System.out.println(e);
        }
        
    }
    
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Maybe you don''t need to implements Externalizable (depends on your needs in fact).

You can simply override the following methods (respecting those exact signatures):

private void writeObject(java.io.ObjectOutputStream out) throws IOException;

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;

For more details, you should refer yourself to the Java API documentation about the Serializable interface.

Share this post


Link to post
Share on other sites
As i mentioned before i did attempt it with Serializable with identical results. I did do the same with writeObject and readObject.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
And have you tried the following ?

public class ExTest extends Panel implements Serializable
{
private void writeObject(ObjectOutputStream out) throws IOException
{
// Do stuff
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
// Do stuff
}

}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
That is right, and Serializable too.
But this is just when restoring the object.

Share this post


Link to post
Share on other sites