File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clarification on concepts for Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Clarification on concepts for Serialization" Watch "Clarification on concepts for Serialization" New topic
Author

Clarification on concepts for Serialization

meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
I just want to clarify some concepts on Serialization. These are my own examples based on K&B
Scenario 1:

In this case when Dog is serialized and deserialized the code will compile fine but will get a runtime exception because A is not serializable.

Scenario 2:

In this case we get NullPointerException because the Animal object comes
back as null after the Dog is deserialized. When you deserialize the Dog the Dog constructor will not run but the Animal constructor will and so gets default values. But that is not the state of the Animal Dog used . Is that why Animal is null?

Scenario 3:

We are ok here.

Scenario 4: involves inheritance

Here if the Dog object has been given values as
Dog d = new Dog(4, “Pete”);
Here the Dog constructor does not run when Dog is deserialized but the animal constructor will. bSo the numberOfLegs will be reset to 0 but the name of the dog is still Pete. But what about the call to super()?
I know this is a long question but I appreciate your input very much,

Thanks,
Meera

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Scenario 2:
When you deserialize the Dog the Dog constructor will not run but the Animal constructor will and so gets default values.

In this case there is NO relationship that is "IS-A"(extends). So there is no point of construtor calling.
the null value is because of the trasisent keyword

Scenario 4
but the name of the dog is still Pete.

Dog class is serializable...So pete(name) is restored.

But what about the call to super()?

super call will ONLY be executed if th DOG construtor is executed.

Correct me if i dont make sense.


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Deepesh Deomurari
Greenhorn

Joined: Oct 29, 2008
Posts: 17
Scenario 1: Every Member object should implement Serializble .

Scenario 2: Every member object initialized by null automatically as part of object and class construction.

Scenario 4: If Dog constructor is not running in deserialization process then HOW that method call will run?

so super(“dogFood”); will never run. REMEMBER transient variables got reset at deserialization process. So constructor of Animal will be called but DOG implements Serializble so constructor of DOG will not be called.

Deepesh Deomurari<br />"When Going Gets Tough,<br />The Tough, Gets Going."
SCJP 1.5 -94%
Deepesh Deomurari
Greenhorn

Joined: Oct 29, 2008
Posts: 17
Scenario 1: Every Member object should implement Serializable .

Scenario 2: Every member object initialized by null automatically as part of object and class construction.

Scenario 4: If Dog constructor is not running in deserialization process then HOW that method call will run?

so super(“dogFood”); will never run. REMEMBER transient variables got reset at deserialization process. So constructor of Animal will be called but DOG implements Serializable so constructor of DOG will not be called.
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
Well here there is a call to super constructor from Dog's constructor


So when Dog is deserialized it's constructor will not run but the Animal class's constructor will. I guess in this case Animal's instance variables will become set to their default values even though you use defaultWriteObject() and defaultReadObject() for Dog. Anyone else have an opinion?(Ankit, Punit, Ruben etc..?)

Thanks,
Meera
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


Try this one.


SCJP 6
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Sorry to resurrect this zombie, but I think it might be worth doing so in this case. I modified Punit's code a little bit:


Output is:
In Animal
Dog name: tommy
Dog numberOfLegs: 10
Dog eat: null

It seems that when you implement readObject(), when deserialization takes place, the first thing that runs is the non-serializable superclass' initialization code. The calls to readInt and readObject are just overwriting the values set by that initialization code.


All code in my posts, unless a source is explicitly mentioned, is my own.
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
So it means that the Animal part of Dog is reset but the Dog's variable -- name is not. Thank you both Punit and Ruben for clarifying this concept.
Regards,
Meera
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clarification on concepts for Serialization
 
Similar Threads
deserialization.....
Deserialization
Nice Serialization Question
Serialization
How Inheritance Affects Serialization