• Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialization and deserialization in inheritance  RSS feed

 
Sushant Kunwara
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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 ?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dint understand your question completely. constructor chaining is independent of the serialization..
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it say anything before that paragraph about constructors not being called?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 19
  • Mark post as helpful
  • send pies
  • 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 ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!