• 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

Serialization and Deserialization

 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If an instance of class B is created, then Serialized, then deSerialized, what would be result?
Ans: java-ranch-java-

Why so...? I didn't get this one?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since A doesn't implement Serializable so its constructor would run when an instance of B is deserialized.

Also serialization is not a part of SCJP anymore...
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Since A doesn't implement Serializable so its constructor would run when an instance of B is deserialized.

Also serialization is not a part of SCJP anymore...



Thanks Ankit,
Could you please explain what Serialization and Deserialization has to do with constructor invocation of classes (class and its super class)?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Also serialization is not a part of SCJP anymore...



WOooooOT???
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swaraj I suggest you read serialization topic from a book as it seems you have very less knowledge of it so you can get complete knowledge by reading a book. But to answer your question, when you deserialize an object, the no-arg constructor of all its ancestor classes which don't implement Serializable interface are invoked. So in this case since class A doesn't implement Serializable, the constructor of A (and java.lang.Object class) will be executed...
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Swaraj , before serializing the B class object you have to create it, and during the constructing of the B object his parents constructors will be invoked with the below output:
java-Ranch-
Then you do your serialization on the B object. During the deserialization you have two upper classes, one implements the Serializable interface and one don't, the later consturctor will run during completing constructing of the deserialized object , at this stage class A (which didn't implements Serializable)constructor will run and print:
java-

Regards
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic