aspose file tools*
The moose likes Java in General and the fly likes Serialization  doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Serialization  doubt" Watch "Serialization  doubt" New topic
Author

Serialization doubt

sai prashanth
Greenhorn

Joined: May 04, 2010
Posts: 18
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

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: May 04, 2010
Posts: 18
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

Joined: May 04, 2010
Posts: 18
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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: May 04, 2010
Posts: 18
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialization doubt