File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Serialization Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Serialization Query" Watch "Serialization Query" New topic
Author

Serialization Query

Deb Dey
Greenhorn

Joined: Mar 31, 2009
Posts: 3
Hi there !

Have a query about serialization. Can someone please help...

If a serializable subclass inherits from a non serializable superclass can we serialize the inherited superclass member variable? I know in the normal case the superclass variable will not be serialized and when deserializing we will get the default value of the data member, but is it possible to have my own writeObject and readObject in the serializable subclass and save the superclass member using writeInt() and read the same by readInt(); I mean the same way we do it for a non serializable class member with a HAS-A relationship.

Thanks,
Deb
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Hi Deb, Welcome to javaranch.

What you are saying looks fine to me as far as I can understand it...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Deb Dey
Greenhorn

Joined: Mar 31, 2009
Posts: 3
Hi Ankit,

I was trying to make it work, but wid no luck

Here I am pasting the code that I was trying. It compiles fine but throws runtime exception. Can you help me see the anomaly, the exception log was not particularly helpful-

/* this uses one object(miao) for serialization
and another object (miao2) for deserialization

Plus we will try to save value of Animal's member: age*/

import java.io.*;

//Non serializable Class
class Animal
{
int age;
}


//Serializable Class
class PetAnimal extends Animal implements Serializable
{
int tailSize;
String name;

PetAnimal(int age, int tailSize, String name)
{ this.age=age;
this.tailSize=tailSize;
this.name=name;
}

//My method for serialization
private void writeObject(ObjectOutputStream os)
{
try
{
os.defaultWriteObject();
System.out.println("defaultWrite successful");
os.writeInt(age);
System.out.println("writeInt successful");
os.close();

}
catch (Exception e)
{
System.out.println("Exception in writeObject");
e.printStackTrace();
}
}


//My method for de-serialization
private void readObject(ObjectInputStream ois)
{
try
{
ois.defaultReadObject();
age= ois.readInt();
ois.close();

}
catch (Exception e)
{
System.out.println("Exception in readObject");
e.printStackTrace();
}
}
}


//Class that serializes
class MyHouse4
{
public static void main(String[] args){
PetAnimal miao=new PetAnimal(1, 2,"cat");

System.out.println("Before Serialization: " + miao.age+" "+miao.tailSize+" "+miao.name);
try{
FileOutputStream fs=new FileOutputStream("PetAnimal.ser");
System.out.println("File is fine");
ObjectOutputStream os= new ObjectOutputStream(fs);
System.out.println("object stream created is fine");
os.writeObject(miao);
System.out.println("object serialized");
os.close();
}
catch (Exception e)
{
System.out.println("Exception in serializing");
e.printStackTrace();

}

//Now deserialize
NewHouse house= new NewHouse();
house.deSerialize();
}
}


//Class that deserializes
class NewHouse
{
PetAnimal miao2=null;
void deSerialize()
{
try{
FileInputStream fis=new FileInputStream("PetAnimal.ser");
ObjectInputStream ois= new ObjectInputStream(fis);
miao2 = (PetAnimal) ois.readObject();
ois.close();
}
catch (Exception e)
{
System.out.println("Exception in de-serializing");
e.printStackTrace();
}
System.out.println("After de-serialization: " + miao2.age+" "+miao2.tailSize+" "+miao2.name);
}
}




The exception log-
C:\Program Files\Java\jdk1.5.0\bin>java MyHouse4
Before Serialization: 1 2 cat
File is fine
object stream created is fine
defaultWrite successful
writeInt successful
Exception in serializing
java.io.IOException: No such file or directory
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:260)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputSt
ream.java:1676)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Obj
ectOutputStream.java:1585)
at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:
1167)
at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:112
1)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.jav
a:1278)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)

at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.jav
a:1392)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:294)
at MyHouse4.main(MyHouse4.java:78)
Exception in de-serializing
java.io.IOException: No such file or directory
at java.io.FileInputStream.read(Native Method)
at java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java
:2200)
at java.io.ObjectInputStream$BlockDataInputStream.peek(ObjectInputStream
.java:2490)
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputSt
ream.java:2500)
at java.io.ObjectInputStream.skipCustomData(ObjectInputStream.java:1865)

at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1839)

at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1
713)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1299)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:339)
at NewHouse.deSerialize(MyHouse4.java:105)
at MyHouse4.main(MyHouse4.java:91)
Exception in thread "main" java.lang.NullPointerException
at NewHouse.deSerialize(MyHouse4.java:113)
at MyHouse4.main(MyHouse4.java:91)

Thanks,
Deb
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
Your error is that your closing the streams inside the implementations of writeObject and readObject. I think you have to re-implement those functions in an exact way.

//try/catch
//invoke default read/write
//read/write extra vars
Mumtaz Khan
Ranch Hand

Joined: Feb 21, 2008
Posts: 53
Deb,
you are closing the os and is stream twice, you should not be closing them in your implementation of writeObject(ObjectOutputStream os) and readObject(ObjectInputStream is). remove it and your code will work fine.

SCJP 5.0 -- 97%
Deb Dey
Greenhorn

Joined: Mar 31, 2009
Posts: 3
Yup its working now !

Thanks everyone ...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Serialization Query
 
Similar Threads
Implementing serializable interface for reference class
Mock: Constructors and Serialization
Serialisation - K&B
How Inheritance Affects Serialization
rules for serialization and a question