Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Serialization  with inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Serialization  with inheritance" Watch "Serialization  with inheritance" New topic
Author

Serialization with inheritance

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
i have a Question in Serialization behaviour in case of non-serializable super class.

Class A ,is non-serializable & Derived class B ,serializable.
Now all the books ,say if object of B is using some of variables of A,to restore A's state correctly while deserialzation of B's object,A must have a no-arg constructor ,other wise Deserialzation fails ,Runtime error comes .

but i tried no ,error comes,B's object after deserialization showing As var values correctly.
Is it something im misunderstanding.
Here is my code...

import java.io.*;
class Electronics extends Devices{
String name="electronicsdevices";
Electronics()
{ name="myelectronic device";
System.out.println("i am constructor of Electronicss");}
}

class TV extends Electronics implements Serializable
{
int make;
TV()
{
make=121;
System.out.println("iam constructor of TV");}

}

public class SerialDemo3 {

public static void main(String args[])
{
TV myTV=new TV();
//myTV.make=2007;

try{
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("TV_object.ser"));
oos.writeObject(myTV);
oos.flush(); oos.close();
}catch(IOException e){}

try{
ObjectInputStream ois=new ObjectInputStream (new FileInputStream("TV_object.ser") );

TV mynewTV=(TV)ois.readObject();
System.out.println(mynewTV+"-> "+mynewTV.make+" "+mynewTV.name); //name is being displayed correctly

}
catch(IOException e){}
catch(ClassNotFoundException c){}

}

}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Lucky J Verma:
...all the books ,say if object of B is using some of variables of A,to restore A's state correctly while deserialzation of B's object,A must have a no-arg constructor ,other wise Deserialzation fails ,Runtime error comes .

but i tried no ,error comes,B's object after deserialization showing As var values correctly...

In your example, A does have a no-args constructor.

In the code below, I modified the superclass Electronics so that it does not have a no-args constructor. You will see the error at runtime. If you uncomment the no-args constructor, then the code will run, but you will see the difference in the resulting object.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
All the books? I had a quick look in Horstmann [C Horstmann, G Cornell, Core Java 2, vol I: fundamentals 7/e Santa Clara: Sun Microsystems Press (2004) pages 671ff] and couldn't find that phenomenon mentioned there. Nor could I find it in the Serializable page of the API. You should always look in the API first. Go through those sources very carefully and check what they actually say.


Anybody else know about this; I had never heard of it before, and Lucky seems to have found it mistaken advice.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Campbell Ritchie:
... Nor could I find it in the Serializable page of the API...

I think the second paragraph is what Lucky is referring to...
To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Yes, I ought to have looked more closely
 
 
subject: Serialization with inheritance