File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Deserialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Deserialization" Watch "Deserialization" New topic


Ameen khan
Ranch Hand

Joined: Jun 10, 2007
Posts: 52
When class is deserialize does its constructor and constructors of its super classes are called or not.

And what if when one of the super classes doesnot implement serialization interface
[ August 22, 2007: Message edited by: Ameen khan ]

SCJP 5.0<br />Next-> I Don't Know
prakash chandra

Joined: Aug 14, 2007
Posts: 24
When a class implements Serializable, its constructor is not called when deserialized. If a super class does not implements Serializable then its constructor is called when deserialized.

SCJP 6.0
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Hi!!! Prakash

Please can you explain the above
with an example,it will be realy
kinda of you,thanks in advance

Preparing SCJP 1.5
Ameen khan
Ranch Hand

Joined: Jun 10, 2007
Posts: 52
class Animal {
public String name;
class Dog extends Animal implements Serializable {
// the rest of the Dog code

Because Animal is NOT serializable, any state maintained in the Animal class, even though the state variable is inherited by the Dog, isn't going to be restored with the Dog when it's deserialized! The reason is, the (unserialized) Animal part of the Dog is going to be reinitialized just as it would be if you were making a new Dog (as opposed to deserializing one). That means all the things that happen to an object during construction, will happen´┐Żbut only to the Animal parts of a Dog. In other words, the instance variables from the Dog's class will be serialized and deserialized correctly, but the inherited variables from the non-serializable Animal superclass will come back with their default/initially assigned values rather than the values they had at the time of serialization.

If you are a serializable class, but your superclass is NOT serializable, then any instance variables you INHERIT from that superclass will be reset to the values they were given during the original construction of the object. This is because the non-serializable class constructor WILL run!

In fact, every constructor ABOVE the first non-serializable class constructor will also run, no matter what, because once the first super constructor is invoked, it of course invokes its super constructor and so on up the inheritance tree
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Thanks Ameen Khan !!!

I am clear with my doubt now!!!
I agree. Here's the link:
subject: Deserialization
Similar Threads
a question about super command
hide and inherit
Generic not compiling
serialization question