• 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 in inheritance

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While reading Head First in java , I encountered this : (pg 443 , 2nd ed )

" If the object has a non-serializable class somewhere up its inheritance tree, the constructor for that non-serializable class will run along with any constructors above that (even if they're serializable).
Once the constructor chaining begins, you can't stop it, which means all superclasses, beginning with the first non-serializable one, will reinitialize their state. "

Look at text in Bold . Now my query is if constructor chaining begins from first non - serializable class , then how can any of its super class be serializable ?
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dint understand your question completely. constructor chaining is independent of the serialization..
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it say anything before that paragraph about constructors not being called?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think the question is about the implementation of the Serializable interface. Meaning once a class implements that interface, how can any of its subclasses *not* implement the interface (meaning be a non-serializable class).

Henry
 
Sushant Kunwara
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Does it say anything before that paragraph about constructors not being called?



Let me just post what was before that paragraph . Yes , Even I am confused about that text - " ... the constructor for that non-serializable class will run along with any constructors above that (even if they're serializable) "

Here's what is on that page . Process of serialization and deserialization was briefed up in 6 points . My problem is in Point 5.


1. The object is read from the stream.

2. The JVM determines (through info stored with the serialized object) the object's class type.

3.The JVM attempts to find and load the object's class. If the JVM can't find and/or load the class, the JVM throws an exception and the deserialization fails .

4.A new object is given space on the heap, but the serialized object constructor does NOT run . Obviously, if the constructor ran, it would restore the state of the object back to its original 'new' state, and that's not what we wont. We want the object to be restored
to the state it had when it was serialized, not when it was first created.

**** 5. If the object has a non-serializable class somewhere up its inheritance tree, the constructor for that non-serializable class will run along with any constructors above that (even if they're serializable). Once the constructor chaining begins, you can't stop it, which means all superclasses, beginning with the first non-serializable one, will reinitialize their state.

6. The object's instance variables are given the values from the serialized state. Transient variables are given a value of null for object references and defaults (0, false , etc.) for primitives.

Hope that makes things clear ...

My query is what does author means to say by - (even if they're serializable) in point 5 ?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
5 years later, and I still don't find an answer for this anywhere on the forum
 
reply
    Bookmark Topic Watch Topic
  • New Topic