Hey, serialization is out of the exam scope.
I will ask for them to move this
thread to a more appropiate forum. Please do not add more replies until it is moved.
Can I give the following line just after line of the method writeObject.
do you mean?
out.defaultWriteObject();
out = new ObjectOutputStream(fos); // assume fos - FileOutputStream.
You can but it has no purpouse. The proper sequence would be:
out = new ObjectOutputStream(fos);
aSerializableObject.writeObject(out);
What is the difference between this approach and Externalizable ? Because, in both the approaches we define our own way of how Object is written and read?
In the default serialization process the private instance fields are also serialized. You must be aware of it and and make them transient if this behaviour does not suit you.
If the object to be serialized has a structure of more objects, most of the times you do not want to serialize the structure but only the content of it. Again, the default serialization will process all the state of an instance: both the "logical" and "physical" state an object. --generally you only want to serialize the "logical" state--
Externalizable does not buy anything to you. You must specify exactly what to serialize and deserialize. See the following URL for an example.
Read more about
serialization by Bruce Eckel.
An impressive book Effective
Java Programming Language Guide, by Joshua Bloch, also deals with the matter.