Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization doubt

 
sai prashanth
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I came to know after going through a chapter on serialization..

1. If a class is serializable then all subclasses can also be serialized.
2. If a class is serializable then its super class can also be serialized BUT there could be complications since the super class constructor will be invoked after the deserialization process.

Now to all those serialization experts if the above two sounds ok, could you tell me why my answer to this question and the one in the answer's section is different.

Question


MY ANSWER
Animal, Dog, Beagle and GSheperd - although Dog is the only class which actually implements serialization, super class Animal can be serialized and all the subclasses too can be serialized - and NOT Living class since it has no relation to the class Dog which implements serializable -- right?

CORRECT ANSWER
Dog and Beagle


I do not get why..any help is appreciated.

Thanks!
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the "BUT" in point 2) means that you may serialize the superclass manually,
but should be aware that the constructor will run on deserialization. So the only
directly serializable (fully automatic) classes are Dog and Beagle. GSheperd is
not serializable because it's its Living member is not.

Jim ... ...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a Sub class is serialized, it doesn't mean that its super class is also serialized. When a subclass is de - serialized, the no-argument Constructor of the super class will run.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49446
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai prashanth wrote: . . . chapter on serialization.. . . .
Please tell us where you got that from; you appear to have misunderstood the chapter, or there was something not clear there.
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:If a Sub class is serialized, it doesn't mean that its super class is also serialized. When a subclass is de - serialized, the no-argument Constructor of the super class will run.

A little more precise, the no-argument constructor of the first non-Serializable parent class will be called. If the super class is also Serializable then its constructor will not be called. In some cases that first parent is java.lang.Object.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
A little more precise, the no-argument constructor of the first non-Serializable parent class will be called. If the super class is also Serializable then its constructor will not be called. In some cases that first parent is java.lang.Object.

 
sai prashanth
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
sai prashanth wrote: . . . chapter on serialization.. . . .
Please tell us where you got that from; you appear to have misunderstood the chapter, or there was something not clear there.


It was a random website on java, and to think of it I am guessing I had misunderstood the statements there.
 
sai prashanth
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Abimaran Kugathasan wrote:If a Sub class is serialized, it doesn't mean that its super class is also serialized. When a subclass is de - serialized, the no-argument Constructor of the super class will run.

A little more precise, the no-argument constructor of the first non-Serializable parent class will be called. If the super class is also Serializable then its constructor will not be called. In some cases that first parent is java.lang.Object.


Thanks for that clarification. Ok, so..as a follow up..

1. Is it true that if a class is serialized then its child classes is also subjected to serialization, unless there is a class which satisfies a -HAS-A- principle and that class is not serializable?
2. What happens if the first parents class is something other than Object and that parent class does not have a no-org constructor, say someone has defined a constructor with args ( which then would not allow for the default no arg constructor to exist unless you define it) ?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai prashanth wrote:
1. Is it true that if a class is serialized then its child classes is also subjected to serialization, unless there is a class which satisfies a -HAS-A- principle and that class is not serializable?


Have a look on this
sai prashanth wrote:
2. What happens if the first parents class is something other than Object and that parent class does not have a no-org constructor, say someone has defined a constructor with args ( which then would not allow for the default no arg constructor to exist unless you define it) ?


Why don't you try it?
 
sai prashanth
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:
sai prashanth wrote:
1. Is it true that if a class is serialized then its child classes is also subjected to serialization, unless there is a class which satisfies a -HAS-A- principle and that class is not serializable?


Have a look on this
sai prashanth wrote:
2. What happens if the first parents class is something other than Object and that parent class does not have a no-org constructor, say someone has defined a constructor with args ( which then would not allow for the default no arg constructor to exist unless you define it) ?


Why don't you try it?


The thread is incomplete and like you I too was not able to get to the bottom of it. Did you ever find out what he meant?
 
Rob Spoor
Sheriff
Pie
Posts: 20552
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:
sai prashanth wrote:
2. What happens if the first parents class is something other than Object and that parent class does not have a no-org constructor, say someone has defined a constructor with args ( which then would not allow for the default no arg constructor to exist unless you define it) ?


Why don't you try it?

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic