• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding Serialization

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kindly use the code tag.
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Aum
can u tell me why 0 arg super constructor is required?
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so it means that it is line below that requires 0 arg constructor because
we are useing casting(cat).

c1 =(cat) ois.readObject();
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic