Forums Register Login

Regarding Serialization

+Pie Number of slices to send: Send
Seethe following code:
import java.io.*;
class Animal
{ int ani;
Animal( int a)
{ System.out.println("Super Class Constructor: a: " +a);
}
}
class cat extends Animal implements Serializable
{ int num;
cat(int a,int b) {
super(100);
num = a ;
ani = b;
}
}
class Hello
{
public static void main ( String args[] )
{
cat c = new cat(2,5);
System.out.println("Before seria : num : " + c.num );
System.out.println("Before seria : ani : " + c.ani );
try {
FileOutputStream fs = new FileOutputStream("test.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(c);
os.close();
} catch(Exception e) { }
try {
FileInputStream fis = new FileInputStream("test.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
c = (cat) ois.readObject();
ois.close();
} catch(Exception e) { }
System.out.println("After seria: num: "+ c.num );
System.out.println("After seria: num: "+ c.ani );
}

}

OutPut is :
Super Class Constructor : a :100
Before seria : num : 2
Before seria : ani : 5
Before seria : num : 2
Before seria : ani : 5

My Doubt is :
Class Animal is nor serializable , so it should print ani value as ZERO after deserialization , and here super class constructor should run. Bur it is not happening.
WHY IT IS LIKE THIS ??
It is working fine when super class has DEFAULT constructor .

Regards,
Sharath
+Pie Number of slices to send: Send
Kindly use the code tag.
+Pie Number of slices to send: Send
The base class data members will be serialized only if the base class itself is serializable, which is not the case in your Animal class.

As the animal class is not serialized, ani will not be serialized.

And more importantly, your base class must have a no arg constructor in order to properly deserialize instances of your derived class.

More info here:
http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html
+Pie Number of slices to send: Send
hi Aum
can u tell me why 0 arg super constructor is required?
+Pie Number of slices to send: Send
As Aum states, it is necessary to write a no-arg constructor in all the super-classes of the class declared as Serializable.

It does give a RuntimeException in your code. Its caught by teh Exception catch handler and as you are printing nothing there, nothing shows up in the console.

And when you are printing the values after deserialization (that actually failed), you are using the same object reference, c. And c has values 2 and 5 for num and ani respectively.

regards,
vijay.
+Pie Number of slices to send: Send
so it means that it is line below that requires 0 arg constructor because
we are useing casting(cat).

c1 =(cat) ois.readObject();
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 931 times.
Similar Threads
Serialization Handing
Constructors called when deserializing properly serialized subclass instance
Serialization question - Superclass is not marked Serializable but the sublass is...
Doubt about Serialization
Serialisation - K&B
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:53:13.