The inheritance _jsp programming of serialization in J2SE

Source: Internet
Author: User
Tags inheritance serialization
When a parent class implements the serializable interface, its subclasses are automatically serialized.

This is verified by the following:

Package serial;
Import java.io.Serializable;
public class Superc implements Serializable {//parent class implements serialization
int supervalue;
Public Superc (int supervalue) {
This.supervalue = Supervalue;
}
Public String toString () {
Return "Supervalue:" +supervalue;
}
}

public class SUBC extends Superc {//Subclass
int subvalue;

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 (100,200);
FileInputStream In=null;
FileOutputStream Out=null;
ObjectInputStream Oin=null;
ObjectOutputStream Oout=null;
try {
out = new FileOutputStream ("Test1.txt");//Subclass serialization
Oout = new ObjectOutputStream (out);
Oout.writeobject (SUBC);
Oout.close ();
Oout=null;

in = new FileInputStream ("Test1.txt");
oin = new ObjectInputStream (in);
SUBC subc2= (SUBC) oin.readobject ();//Sub-class deserialization
System.out.println (SUBC2);
catch (Exception ex) {
Ex.printstacktrace ();
} finally{
... omitted here
}
}
}

The results of the operation are as follows:

supervalue:100 sub:200

The visual subclass succeeded in serialization/deserialization.

How to implement serialization of subclasses seems like a simple thing to do. Sometimes, however, we cannot allow the parent class to implement the Serializable interface, because the parent class is abstract (this does not matter), and the parent class is not able to enforce the ability to serialize each subclass. In other words, the purpose of the parent design is simply to be inherited.

It is a hassle to write a subclass that can be serialized for a parent class that does not implement the serializable interface. Mentioned in the Java docs:

"To allow subtypes of non-serializable classes to is serialized, the subtype may assume responsibility for saving and rest Oring 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 a accessible No-arg constructor to initialize The class ' s state. It is an error to declare a class Serializable if It isn't the case. The error is detected at runtime. ”

In other words, to write a subclass that can be serialized for a parent class that does not implement the Serializable interface, do two things:

First, the parent class should have an constructor;

Second, subclasses are responsible for serializing (deserializing) the domain of the parent class.

We remove the Superc serializable interface and add the serializable interface to the SUBC. Error occurred after running:

java.lang.Error:Unresolved compilation Problem:
Serializable cannot be resolved or are not a valid Superinterface
At SERIAL.SUBC. (subc.java:15)
At Serial.Test1.main (test1.java:19)
Exception in thread "main"

As is true in docs, it is not possible for the parent class to be missing a parameterless constructor.

Next, we'll rewrite This example as suggested by docs:

Public abstract class Superc {
int supervalue;
Public Superc (int supervalue) {
This.supervalue = Supervalue;
}
The public Superc () {}//adds an parameterless constructor
Public String toString () {
Return "Supervalue:" +supervalue;
}
}

public class SUBC extends Superc implements Serializable {
int subvalue;

Public subc (int supervalue,int subvalue) {
Super (Supervalue);
This.subvalue=subvalue;
}

Public String toString () {
return super.tostring () + "sub:" +subvalue;
}

private void WriteObject (Java.io.ObjectOutputStream out)
Throws ioexception{
Out.defaultwriteobject ()//first serialization of objects
Out.writeint (Supervalue);//serialize the domain of the parent class
}
private void ReadObject (Java.io.ObjectInputStream in)
Throws IOException, classnotfoundexception{
In.defaultreadobject ()//Deserialize object first
Supervalue=in.readint ()//deserialize the domain of the parent class
}
}

The results of the operation prove that this method is correct. Here we use the Writeobject/readobject method, which, if it exists, is invoked when serialized, in place of the default behavior (which is discussed later, so much more). When we serialize, we first invoke the ObjectOutputStream Defaultwriteobject, which uses the default serialization behavior and then serializes the domain of the parent class, as well as when deserializing.

Sum up:

Purpose behavior

For a parent class that implements the Serializable interface, write a subclass subclass that can be serialized to automatically serialize

For a parent class that does not implement the Serializable interface, write a subclass of 1 that can be serialized, a parent class that has an constructor;2, a subclass that serializes itself, and the subclass is responsible for serializing the domain of the parent class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.