Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes I/O and Streams and the fly likes how  FileOutputStream and ObjectOutputStream working here Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "how  FileOutputStream and ObjectOutputStream working here" Watch "how  FileOutputStream and ObjectOutputStream working here" New topic
Author

how FileOutputStream and ObjectOutputStream working here

pawni jain
Greenhorn

Joined: Apr 10, 2008
Posts: 22
hello ranchers
please explain me how the FileOutputStream and ObjectOutputStream working in the following code..

import java.io.*;
class Player {
Player() { System.out.print("p"); }
}
public class CardPlayer extends Player implements Serializable {
CardPlayer() { System.out.print("c"); }
public static void main(String[] args) {
CardPlayer c1 = new CardPlayer();
try {
FileOutputStream fos = new FileOutputStream("play.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(c1);
os.close();
FileInputStream fis = new FileInputStream("play.txt");
ObjectInputStream is = new ObjectInputStream(fis);
CardPlayer c2 = (CardPlayer) is.readObject();
is.close();
} catch (Exception x ) { }
}
}

output:
pcp

Thanks,
Regards.
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
1) A class can be serialized even if its parent class is not.
2) When an instance of a Serializable class is deserialized, the constructor does not run, and instance variables arc not given their initially assigned values.
3) If your class is a serializable class, but your superclass is not serializable, then the non-serializable class constructor will run when the object for child class is getting deserialized. Every constructor above the first non-serializable class constructor will also run, no matter what, because once the first super constructor is invoked, it of course invokes its super constructor and so on up the inheritance tree

For the output:
The first 'p' and 'c' in the output are simple - super class constructor and class constructor getting called while object creation of class CardPlayer in:
CardPlayer c1 = new CardPlayer();

The last p is because of point 3 stated above.
pawni jain
Greenhorn

Joined: Apr 10, 2008
Posts: 22
hello Amitabh

Thanks for explaining the reason.I got the concept.


Thanks ,
Regards.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

One addition to amitabh's explanation:

Only the constructor without parameters is called for the direct super class. If there is none you will get an InvalidClassException, which is an indirect subclass of IOException.

For example:

Class C can be serialized and deserialized without a problem. However, once you omit line 1, it can only be serialized.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: how FileOutputStream and ObjectOutputStream working here
 
Similar Threads
Related to Object Serialization
Deserializing
serialization question
Can you help me to tell the output of this program and how we are getting this....
About I/o Files