• Post Reply Bookmark Topic Watch Topic
  • New Topic

About I/o Files

 
Ashish Anil Khadilkar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a bit confused with this program.The output of this program I expected as 'pc' but the correct output is 'pcp'. can pls anybody explain me how it works ? thanks

import java.io.*;
class Player {
Player() { System.out.print("p"); }
}
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 ) { }
}
}
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is in the javadocs for ObjectInputStream.

I'll give you a hint what to look for: The Player class does not implement the Serializable interface.
[ May 07, 2007: Message edited by: Ulf Dittmer ]
 
Ashish Anil Khadilkar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please Explain me in brief ? If SuperClass player implements Serializable interface then it shows the output 'pc' as expected.But if Serilizable is not implementated by superclass then what make it to change output.Could you pls explain it .Thanks
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read the javadocs as I suggested? It specifically mentions the difference between serializing a class that implements Serializable, and serializing one that doesn't.
 
Ashish Anil Khadilkar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have gone through the javaDocs but still I didn't understand the reason behind how it works!!! If you don't mind can you please provide some lights on this.
thanks in advance .
 
Ashish Anil Khadilkar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i am not wrong this is what i undestand :
When the serial data is being read from the file... an over ridable instance is created...
hence, only Player object is created and it can be mapped on to CardPlayer object
since there is no card player object is created... there is no invocation of card player

Thanks
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, no.

The crucial sentence is this one: "No-arg constructors are invoked for the non-serializable classes".

You're trying to explain the extra "p", which indicates that the Player constructor is being called when you didn't expect it. That sentence explains why it is being called - because Player does not implement Serializable. As soon as it does, the constructor is no longer being called and the "p" doesn't show up, as you have found out.
 
Ashish Anil Khadilkar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf ...
Now I understand the concept behind that..
Thank you once again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!