Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
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
Forum:
Programmer Certification (OCPJP)
serialization question
William Yan
Ranch Hand
Posts: 69
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi ranchers,
I saw this in a K&B exam.
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) { } } }
The output is 'pcp'.
I don't know why the 2nd 'p' is printed.
Thankyou.
Java is better and better, yet bitter and bitter.
Bing Qiao
Greenhorn
Posts: 21
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The base class doesn't implement Serializable Interface which means the constructor of the base class will be called during the deserialization of the subclass.
William Yan
Ranch Hand
Posts: 69
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
^O^
Got it.
Thanks a lot.
Java is better and better, yet bitter and bitter.
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Questions from K&B 6
how FileOutputStream and ObjectOutputStream working here
Serialization
Related to Object Serialization
Deserializing
More...