PDA

Click to See Complete Forum and Search --> : on object serialization


quipy
Apr 30th, 2003, 07:12 AM
one abstract superclass

three subclasses derived from above

one driver class with static main

which of the five classes should have "implements Serializable"? when i add it to the superclass and subclasses, i get errors that say "Interface Serializable of class (class name) not found" for each of the classes.

CreoN
Apr 30th, 2003, 07:16 AM
Some code would definitely help :)
One wild guess: Did you import java.io.* or java.io.Serializable ?

CornedBee
Apr 30th, 2003, 06:02 PM
The class that you want to be serializable should implement Serializable.

Probably the abstract and all subclasses.

Is the error a compile time or run time error?

MC_Claus
May 7th, 2003, 02:42 AM
When you want to serialize an object you would have to implements Serializable in the superclass. All the classes that inherit from this superclass will have the Serializable interface implemented, that is what lies in inheritance in Java :)

Notice that if you want to be sure what it serializes you have to implement to methods on your own. That is propaply the only way that you can be sure which variables in your object is serialized. The two methods that you have to implement is:


/**
* This method is the one that saves your object
* It is an example from one of my smaller projects, but hope you
* can use it
*/
private void writeObject( ObjectOutputStream out ) throws IOException {
out.writeObject( new Date() );

out.writeInt( size );
out.writeInt( numberOfEdges );
out.writeBoolean( oriented );
out.writeBoolean( weighted );
out.writeBoolean( isOK );
out.writeObject( graph );
}
/**
* This is the method that reads your object again.
* Remember that you have to read the variables in the same
* order that you saved them.
*/
private void readObject( ObjectInputStream in )
throws IOException, ClassNotFoundException
{
System.out.println("Class saved at: " + (Date)in.readObject());

size = in.readInt();
numberOfEdges = in.readInt();
oriented = in.readBoolean();
weighted = in.readBoolean();
isOK = in.readBoolean();
graph = (Graph)in.readObject();
}


And notice: If you have aggregation to any object you want to serialize then they have to implement Serializable as well. If you don't want to implements the two above methods you don't have to!! They are mostly used by people who want total control over what happens (like me :D). The Serializabe interface doesn't contain any methods, so you can just as easily use the default implementation that Java provides. In most cases that is enough.

When you then want to serialize your object, or get an object from a file, you can use these two methods, i.e. from the main method, just copy them into the class where you have the static void main method:

/**
* When you want to read an object from a file simple call this
* method with the exact filename as a parameter i.e.
* readObjectFromFile( "MyClass.ser" );
*/
private static Object readObjectFromFile( String filename ) throws Exception {
FileInputStream fileIn = new FileInputStream( filename );
ObjectInputStream objectIn = new ObjectInputStream( fileIn );

Object obj = objectIn.readObject();

fileIn.close();

return obj;
}
/**
* When you want to "save" your object to a file simple give the
* filename and a class which have implemented Serializable i.e.
* writeObjectToFile("MyClass.ser", myClass )
*/
private static void writeObjectToFile( String filename, Serializable obj )
throws Exception
{
FileOutputStream fileOut = new FileOutputStream( filename );
ObjectOutputStream objectOut = new ObjectOutputStream( fileOut );

objectOut.writeObject( obj );

fileOut.close();
}


Hope that is enough to get you started otherwise I will be happy to help out again. :D :cool: