[java] Overriding default Serializable

Started by
4 comments, last by MOVSW 19 years, 12 months ago
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);
        }
        
    }
    
}
Advertisement
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.
As i mentioned before i did attempt it with Serializable with identical results. I did do the same with writeObject and readObject.
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
}

}
Doesn''t Externalizable require a public no-arg constructor ?
That is right, and Serializable too.
But this is just when restoring the object.

This topic is closed to new replies.

Advertisement