There are two cases for Java serialization in class hierarchy.
Suppose we have two classes : SuperC and SubC. ( It does not
matter whether SuperC is abstract class or not)
Case 1: Super class (SuperC) implements interface "Serializable".
This case is simple. The sub class does not need to implement interface "Serializable".
Case 1: Super class (SuperC) does not implement interface "Serializable".
This case is complex. The sub class implements interface "Serializable".
It has to meet two requirements.
(1) there is a no-arg constructor in super class
(2) the sub class needs to be in charge of serializing and deserializing
the super class filed. (define writeObject and readObject methods).
The code example is as follows.
Reference links:
http://www.javapractices.com/topic/TopicAction.do?Id=70
http://hi.baidu.com/ccboyhi/blog/item/c0e60ff506fff9f17609d7d1.html
Suppose we have two classes : SuperC and SubC. ( It does not
matter whether SuperC is abstract class or not)
Case 1: Super class (SuperC) implements interface "Serializable".
This case is simple. The sub class does not need to implement interface "Serializable".
package serializable; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; abstract class SuperC implements Serializable { int supervalue; public SuperC() { } public SuperC(int supervalue) { this.supervalue = supervalue; } public String toString() { return "supervalue: " + supervalue; } } class SubC extends SuperC { int subvalue; private static final long serialVersionUID = 201111291028L; public SubC(int supervalue, int subvalue) { super(supervalue); this.subvalue = subvalue; } public String toString() { return super.toString() + " sub: " + subvalue; } } public class Test1 { public static void main(String[] args) { SubC subc = new SubC(10, 20); FileInputStream in = null; FileOutputStream out = null; ObjectInputStream oin = null; ObjectOutputStream oout = null; try { out = new FileOutputStream("TestFile.txt"); oout = new ObjectOutputStream(out); oout.writeObject(subc); oout.close(); oout = null; in = new FileInputStream("TestFile.txt"); oin = new ObjectInputStream(in); SubC subc2 = (SubC)oin.readObject(); System.out.println(subc2); } catch (Exception ex) { ex.printStackTrace(); } finally { } } }
Case 1: Super class (SuperC) does not implement interface "Serializable".
This case is complex. The sub class implements interface "Serializable".
It has to meet two requirements.
(1) there is a no-arg constructor in super class
(2) the sub class needs to be in charge of serializing and deserializing
the super class filed. (define writeObject and readObject methods).
The code example is as follows.
package serializable; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class SuperC { int supervalue; public SuperC() { } public SuperC(int supervalue) { this.supervalue = supervalue; } public String toString() { return "supervalue: " + supervalue; } } class SubC extends SuperC implements Serializable { int subvalue; private static final long serialVersionUID = 201111291028L; public SubC(int supervalue, int subvalue) { super(supervalue); this.subvalue = subvalue; } private void writeObject(ObjectOutputStream aStream) throws IOException { aStream.defaultWriteObject(); //manually serialize superclass aStream.writeObject(supervalue); } private void readObject( ObjectInputStream aStream) throws IOException, ClassNotFoundException { aStream.defaultReadObject(); //manually deserialize and init superclass supervalue = (Integer)aStream.readObject(); } public String toString() { return super.toString() + " sub: " + subvalue; } } public class Test1 { public static void main(String[] args) { SubC subc = new SubC(10, 20); FileInputStream in = null; FileOutputStream out = null; ObjectInputStream oin = null; ObjectOutputStream oout = null; try { out = new FileOutputStream("TestFile.txt"); oout = new ObjectOutputStream(out); oout.writeObject(subc); oout.close(); oout = null; in = new FileInputStream("TestFile.txt"); oin = new ObjectInputStream(in); SubC subc2 = (SubC)oin.readObject(); System.out.println(subc2); } catch (Exception ex) { ex.printStackTrace(); } finally { } } }
Reference links:
http://www.javapractices.com/topic/TopicAction.do?Id=70
http://hi.baidu.com/ccboyhi/blog/item/c0e60ff506fff9f17609d7d1.html
Comments
Post a Comment