• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Deserialization

 
Ameen khan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
prakash chandra
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!!! Prakash

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



Preparing SCJP 1.5
 
Ameen khan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ameen Khan !!!

I am clear with my doubt now!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic