• 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

Serializable interface

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can someone please explain the O/P of the following code from K & B book:


The output is pcp. I understand the first character p in the output as the superclass Player is not serializable so it constructor should run.... but my query is why does the constructor of the subclass CardPlayer run? CardPlayer is a serializable subclass of Player...if its constructor runs then it initializes the CardPlayer and doesn't restore it back to its last state(deserialize it).Also why does constructor of Player runs after subclass's constructor..


Thanks in advance






[/CODE]
[ December 17, 2007: Message edited by: Jesper Young ]
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
apologies for printing the query within the code block...


The output is pcp. I understand the first character p in the output as the superclass Player is not serializable so it constructor should run.... but my query is why does the constructor of the subclass CardPlayer run? CardPlayer is a serializable subclass of Player...if its constructor runs then it initializes the CardPlayer and doesn't restore it back to its last state(deserialize it).Also why does constructor of Player runs after subclass's constructor..

Thanks
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "c" prints out because of this line.



If the constructor of CardPlayer were called again when the object was deserialized, you would see 2 c's instead of 1.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: You can edit your own posts afterwards by clicking the icon.
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the clarification with the below snippet



The call to the CardPlayer constructor will call the super constructor Player:



Dont you think the O/P should be ppc instead of pcp,What I don't get is how the superclass constructor is called again when the SOP statement

has executed?

Please can someone explain what I am missunderstanding?

Many Thanks in advance
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First the Player constructor is invoked, as a superclass constructor, and prints "p".
Then the CardPlayer constructor prints "c".
Then the serialization happens, and during the deserialization, the Player constructor is invoked again (printing "p"), because the Player class is a subclass of CardPlayer, but the Player class is not Serializable, so it is initialized in a standard way.
The CardPlayer constructor doesn't run second time, because it is declared Serializable.
[ December 17, 2007: Message edited by: Serge Petunin ]
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the explaination... I was assuming that the serialization process starts as soon I run the class but I understand now that the deserialization process starts no sooner when the readObject() method is called which calls the non serializable constructor..before the readObject()invocation normal constructor chaining process takes place..

Please correct If I am wrong here

Thanks
 
Sergey Petunin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, serialization occurs only when you run the writeObject method, and deserialization is started with the readObject method.

Otherwise the CardPlayer class is no different from any other class. So when you say:

you just create an instance of the class as usual, no serialization/deserialization happens here.
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks again for the reassertion
 
reply
    Bookmark Topic Watch Topic
  • New Topic