Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Serialization" Watch "Regarding Serialization" New topic
Author

Regarding Serialization

babu sharath
Ranch Hand

Joined: Jul 06, 2006
Posts: 42
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
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
Kindly use the code tag.


SCJP 1.4 85%
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
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
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
hi Aum
can u tell me why 0 arg super constructor is required?
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
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.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
so it means that it is line below that requires 0 arg constructor because
we are useing casting(cat).

c1 =(cat) ois.readObject();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Serialization