Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Serialization" Watch "Serialization" New topic
Author

Serialization

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
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

Joined: Nov 12, 2008
Posts: 757
Please Quote Your Sources.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Treimin Clark wrote:Please Quote Your Sources.

Look at his last 2 words in the quote. But to please you: JAVA API


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I think this code proves the statement

alex sandoval
Greenhorn

Joined: Dec 08, 2008
Posts: 26
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

Joined: Sep 16, 2008
Posts: 509
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

Joined: Dec 08, 2008
Posts: 26
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

Joined: Oct 25, 2008
Posts: 2700

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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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

Joined: Dec 08, 2008
Posts: 26
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

Joined: Apr 23, 2007
Posts: 180
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

Joined: Apr 23, 2007
Posts: 180
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

Joined: Dec 08, 2008
Posts: 26
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

Joined: Dec 16, 2008
Posts: 1032
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.


All code in my posts, unless a source is explicitly mentioned, is my own.
alex sandoval
Greenhorn

Joined: Dec 08, 2008
Posts: 26
Now I got it! Thanks to you guys.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Serialization
 
Similar Threads
JQ+: I/O QUESTION
regarding Serializable
Non-serializable Object to Byte Array
Serialization with inheritance
Serialization