Monday 29 August 2016

Java - Serializable

11)    Super class with Serializable, will serialize all the objects of the subclass objects.
22)    If Subclass is Serializable interface implemented, then that will serialize all the objects of the super class objects.

You can think of serialization as the process of converting an object instance into a sequence of bytes (which may be binary or not depending on the implementation).
It is very useful when you want to transmit one object data across the network, for instance from one JVM to another.
In Java, the serialization mechanism is built into the platform, but you need to implement theSerializable interface to make an object serializable.
You can also prevent some data in your object from being serialized by marking the attribute astransient.
Finally you can override the default mechanism, and provide your own; this may be suitable in some special cases. To do this, you use one of the hidden features in java.
It is important to notice that what gets serialized is the "value" of the object, or the contents, and not the class definition. Thus methods are not serialized.
Here is a very basic sample with comments to facilitate its reading:
import java.io.Serializable;

// This class implements "Serializable" to let the system know
// Which ever the class extends this class, are also eligible for Serialization.
public class SerializeOne implements Serializable {
    private int ii = 10;

    public int getIi() {
        return ii;
    }

    public void setIi(int ii) {
        this.ii = ii;
    }
}


import java.io.*;
import java.util.*;

public class SerializeEx extends SerializeOne {

    // These attributes conform the "value" of the object.

    // These two will be serialized;
    private String aString = "The value of that string";
    private int    someInteger = 0;

    // But this won't since it is marked as transient.
    private transient List<File> unInterestingLongLongList;

    // Main method to test.
    public static void main( String [] args ) throws IOException  {

        // Create a sample object, that contains the default values.
        SerializeEx instance = new SerializeEx();

        // The "ObjectOutputStream" class have the default
        // definition to serialize an object.
        ObjectOutputStream oos = new ObjectOutputStream(
                // By using "FileOutputStream" we will
                // Write it to a File in the file system
                // It could have been a Socket to another
                // machine, a database, an in memory array, etc.
                new FileOutputStream(new File("o.ser")));

        // do the magic
        oos.writeObject( instance );
        // close the writing.
        oos.close();

        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("o.ser")))) {

            SerializeEx ex = (SerializeEx)in.readObject();
            System.out.println(ex.getIi());

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
 
When we run this program, the file "o.ser" is created and we can see what happened behind.
If we change the value of: someInteger to, for example Integer.MAX_VALUE, we may compare the output to see what the difference is.
Here's a screenshot showing precisely that difference:

Can you spot the differences? ;)
There is an additional relevant field in Java serialization: The serialversionUID but I guess this is already too long to cover it.




No comments:

Post a Comment