Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization with inheritance

 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 51413
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51413
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I ought to have looked more closely
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic