File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Serialization

 
Abhi vijay
Ranch Hand
Posts: 509
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. JAVA API


Can someone give me an example to prove the above statement?
 
Treimin Clark
Ranch Hand
Posts: 757
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please Quote Your Sources.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Treimin Clark wrote:Please Quote Your Sources.

Look at his last 2 words in the quote. But to please you: JAVA API
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this code proves the statement

 
alex sandoval
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Wouter, this is a very good example.

But I have a question. When I removed the comments (see modified code below) and compiled and executed it, it gives me InvalidClassException when the out.writeObject() line is executed. So I decided that it is using the defaultWriteObject() of the ObjectOutputStream class so I created my own implementation of writeObject() but still it gives me InvalidClassException. What is the reason behind this?

 
Abhi vijay
Ranch Hand
Posts: 509
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I have a question. When I removed the comments (see modified code below) and compiled and executed it, it gives me InvalidClassException when the out.writeObject() line is executed. So I decided that it is using the defaultWriteObject() of the ObjectOutputStream class so I created my own implementation of writeObject() but still it gives me InvalidClassException. What is the reason behind this?


Hi, alex, The reason you are getting InvalidClass Exception is because class A does not have a no-arg constructor. You implement writeObject() and readObject() in your class, when you want to manually insert the values of certain variables(which are transient). In this case since A is non-serializable, during deserialization process of Object b , the initialization process(initialization of instance variables(in this case none) and constructor completion) happens all over again, for which you need the default constructor. In the above code, there is no default constructor in class A, so Error.
 
alex sandoval
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Abhi.

But why is it that when you run the code above the problem seems to be the class B?

Exception in thread "main" java.io.InvalidClassException: B; B; no valid constructor
at java.io.ObjectStreamClass.checkDeserialize(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at SerialisationTest.main(OverrideTest.java:26)
Caused by: java.io.InvalidClassException: B; no valid constructor
at java.io.ObjectStreamClass.<init>(Unknown Source)
at java.io.ObjectStreamClass.lookup(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at SerialisationTest.main(OverrideTest.java:22)
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's because you're trying to serialize a object of type B. But B can't be serialized because it's super doesn't have a default constructor.
The exception is a little bit confusing.

Edit: Why did you insert those 2 functions?
 
Ankit Garg
Sheriff
Posts: 9495
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you would have tried, then you must have noted that there is no exception while serialization. The exception occurs during deserialization as A is not serializable so the JVM tries to call a no-arg constructor for A which is not found resulting in an exception...
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your right. I was confused by
at SerialisationTest.main(SerialisationTest.java:7)
that references to "out.writeObject(b);".

Thanks, learned a bit.
 
alex sandoval
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:If you would have tried, then you must have noted that there is no exception while serialization. The exception occurs during deserialization as A is not serializable so the JVM tries to call a no-arg constructor for A which is not found resulting in an exception...


Hi Ankit, regarding your statement above, does it mean that when the superclass is not serializable the JVM will instantiate an instance of it even though we are just concerned with the class B during deserialisation process? I thought deserialisation process will only desirialize the object that it is concerned to which is in this case the class B.

Hence, when the deserialization instantiates class B it will call its no-arg constructor which will call class A contructor passing an int value to it. In this case the instantiation of class A should be fine because its subclass is handling it.

Am I wrong or I am just confused? ops:
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While deserialization the subclass calls super class no-arg constructor???

import java.io.*;
class Food{
Food(){
System.out.println("1");
}
}
class Fruit extends Food implements Serializable{
Fruit(){
super();
System.out.println("2");
}
}




public class SerializationEx extends Fruit{


SerializationEx() {
super();
// TODO Auto-generated constructor stub
}

/**
*
*/

int size=42;

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SerializationEx s=new SerializationEx();
try{
FileOutputStream fs=new FileOutputStream("a.ser");
ObjectOutputStream os=new ObjectOutputStream(fs);
os.writeObject(s);
}catch(FileNotFoundException e){
System.out.println("filenotfoundexception");
}
catch(IOException e){
System.out.println(e);
}
try{
FileInputStream fis=new FileInputStream("a.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
SerializationEx s1=(SerializationEx)ois.readObject();
System.out.println(s1.size);
}catch(FileNotFoundException e){
System.out.println("filenotfound");
}
catch(IOException e){
System.out.println("io");
e.printStackTrace();
}
catch(ClassNotFoundException e){
System.out.println("classcast");
}
}
}




The out put for this is :
1
2
1 // is this from Food Constructor??? If so why Fruit constructor is not called???
42



 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the point.....................

while deserialization the object , if the super class is not serializble then it's no-arg constructor will be called and if it is not there then we will get Exception. Am I Right ??
 
alex sandoval
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Banu Chowdary wrote:I got the point.....................

while deserialization the object , if the super class is not serializble then it's no-arg constructor will be called and if it is not there then we will get Exception. Am I Right ??


Hi Banu, why will the JVM call the class A's no-arg contructor if it is just concerned of deserializing the class B object?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex,

When you deserialize an object of a class that has a non-serializable superclass, the superclass' no-arg constructor will be called implicitly to initialize the superclass instance data portion of the object. That, plus all the instance initialization code (instance initializer statements and instance initialization blocks) will run. So, if your non-serializable superclass doesn't have a no-arg constructor it won't work.
 
alex sandoval
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I got it! Thanks to you guys.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic