This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Distributed Java and the fly likes will subclass gets serialized? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Distributed Java
Reply Bookmark "will subclass gets serialized?" Watch "will subclass gets serialized?" New topic
Author

will subclass gets serialized?

prashanth mallik
Greenhorn

Joined: Jan 21, 2001
Posts: 3
If class A implements serializable ,then does class B which is subclass of class A also gets serialized?
Jerry Pulley
Ranch Hand

Joined: Sep 19, 2000
Posts: 221
Prashanth,
Yes, all subclasses of a serializable class are also serializable.
Jerry
prashanth mallik
Greenhorn

Joined: Jan 21, 2001
Posts: 3
Is it other way also, i mean to say
If Class B implements Serializable, does Class A super class of class B also serialized.


prashanth mallik
Greenhorn

Joined: Jan 21, 2001
Posts: 3


[This message has been edited by prashanth mallik (edited January 22, 2001).]
Jerry Pulley
Ranch Hand

Joined: Sep 19, 2000
Posts: 221
Prashanth,
Man, I thought I was going to get off easy on this one.
No, the non-serializable superclass of a serializable class is not serialized. Notice I say "the", not "a", because at some point in every class's parentage there is a non-serializable class - Object is not serializable.
There are a couple of points to address here:
1) Suppose C is serializable and B, its immediate superclass, is not. Then B must provide a no-argument constructor, and the constructor must be accessible by C. When C is deserialized, B() will be called to construct the superclass part of C's state. If B doesn't have such a c'tor, deserializing C will throw an InvalidClassException. You won't see any problems when you serialize C, only when you attempt to deserialize it.
2) It is possible to include superclass fields in serialization by overriding writeObject() and readObject() in the serializable class, so long as the fields are accessible. These methods give you the opportunity to save and restore superclass state beyond the default values provided by the superclass's no-arg c'tor.
Jerry
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: will subclass gets serialized?
 
Similar Threads
Method invocation with null argument
action does not declare serialversionuid warning
serialization question
Serialization doubt
Serialization via Inheritance