Externalizable in java

This is 3rd part of java Serialization tutorial

Java Serialization Tutorial:

Before understanding Externalizable interface, you need to have idea about Serialization.You can read more about Serialization at Serialization in java.
Java provides mechanism called serialization to persists java objects in a form of ordered or sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object.

Externalizable:

As name suggest it is externalilizing your serialization.If you want to customize your serialization mechanism then you can use it.It uses custom written mechanism to perform marshalling and unmarshalling of objects.Externalizable interface extends Serializable interface. If you implement this interface then you need to override following methods.

Now lets see how serialization happens:

At sender side:
JVM checks if class implements externalizable or not.If it does then serialize object using writeExternal() method.If it does not implement externalizable but implements serializable , object is serialized using ObjectOutputStream.

At receiver side:
When object is reconstructed and it is externalizable , an instance is created using no args constructor and readExternal is called.If it is not externalizable but serializable , object is reconstructed using ObjectInputStream.

Lets start with example same as we have used in Serialization in java.
Create Employee.java in src->org.arpit.java2blog

Employee.java:

you must have no args contructor if you implement externalizable.

Create ExternalizableMain.java in org.arpit.java2blog
ExternalizableMain.java:
Run it :

When you run ExternalizableMain.java.You will get following output

If you already have serializable,why you need externalizable at all!!:

  • When you serialize any object using serializable, apart from fields, all objects that belong to object map and that can be reached using instance variable will also be serialized .for example :
    • If you have Employee class and its superclass is person then it will serialize all superclass objects (such as person) until it reaches “Object” class.
    • Similarly if Employee has instance variable of address class then it will serialize whole object map of address also .

Do you really want this much overhead when all you want to serialize is employeeId and  employeeName

  • JVM uses reflection when you use serializable which is quite slow.
  • While serializing,information about class description which incluses description of its superclass and instance variable associated with that class also get stored in stream.Again this is also a performance issue

Inheritance in Externalization:

Now we will see how inheritance affects externalization.So there can be muliple cases whether super class is externalizable or not.If not then how will you handle that and how it works.Lets see by example.
We will create Person.java which will be superclass of Employee.

Case 1: What if super class does not implement Externalizable:

If superclass does not implements externalizable , you need to serialize superclass ‘s fields in subclass that implements Externalizable.

Person.java

Create Employee.java in org.arpit.java2blog
Employee.java:

Create ExternalizableMain.java in org.arpit.java2blog

ExternalizableMain.java:

Run it :

When you run ExternalizableMain.java.You will get following output:

Case 2: What if super class implements Externalizable:

If superclass implements externalizable ,then it will also have readExternal() and writeExternal() method so it will serialize its own fields in these methods

Person.java

Create Employee.java in org.arpit.java2blog
Employee.java:

Create ExternalizableMain.java in org.arpit.java2blog

ExternalizableMain.java:

Run it :

When you run ExternalizableMain.java.You will get following output:

In this example, since the Person class stores and restores its fields in its own writeExternal and readExternal methods, you dont need to save/restore the superclass fields in sub class but if you observe closely the writeExternal and readExternal methods of Employee class, you will find that you still need to first call the super.xxxx() methods that confirms the statement the externalizable object must also coordinate with its supertype to save and restore its state.

DownSides of Externalizable:

  • If you make any change to your class definition, you need to maintain writeExternal() and readExternal accordingly.
  • As we have seen in example,Sub class object has to coordinate with its superclass to save and store its state(by call super.xxxx() method from subclass)

Was this post helpful?

Comments

  1. Hi Arpit,

    Its good initiative I appreciate that. I have some questions in my mind.

    In case 1 of inheritance what if I don't choose to serialize the fields of parent class and stilĺ I try to deserilize thode fields in main method, will I get any exception?

    In case 2, what if I dont call super.xxx () in readExternal and writeExternal method. Will the jvm calls parents overridden methods by default?

  2. Hi Avinash,
    Thanks
    In case 1- No it won't give you exception,it will give you default values of fields of Parent class.
    In Case 2- JVM won't call parent's overridden methods by default. You need to explicitly call it

Leave a Reply

Your email address will not be published. Required fields are marked *