Explain Externalizable in Java

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 5 Comments on Explain Externalizable in Java

Explain Externalizable in Java

We have seen an example of Serialization and how it works in our previous article. However there is one more interface provided by the Java API which is “Externalizable“. This interface extends “Serializable” and provides two more methods as shown below :

  1. void readExternal(ObjectInput in)
  2. void writeExternal(ObjectOutput out)

Externalizable interface (from API):

Only the identity of the class of an Externalizable instance is written in the serialization stream and it is the responsibility of the class to save and restore the contents of its instances. The writeExternal and readExternal methods of the Externalizable interface are implemented by a class to give the class complete control over the format and contents of the stream for an object and its supertypes. These methods must explicitly coordinate with the supertype to save its state. These methods supersede customized implementations of writeObject and readObject methods.
Object Serialization uses the Serializable and Externalizable interfaces. Object persistence mechanisms can use them as well. Each object to be stored is tested for the Externalizable interface. If the object supports Externalizable, the writeExternal method is called. If the object does not support Externalizable and does implement Serializable, the object is saved using ObjectOutputStream.
When an Externalizable object is reconstructed, an instance is created using the public no-arg constructor, then the readExternal method called. Serializable objects are restored by reading them from an ObjectInputStream.
An Externalizable instance can designate a substitution object via the writeReplace and readResolve methods documented in the Serializable interface.

Example :

package com.G2.Serialization;

import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Demo1 implements Externalizable {
	public Demo1() {
		System.out.println("Demo1 Constructor");
	}

	public void writeExternal(ObjectOutput out) throws IOException {
		System.out.println("Demo1 writeExternal()");
	}

	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		System.out.println("Demo1 readExternal()");
	}
}

class Demo2 implements Externalizable {
	public Demo2() {
		System.out.println("Demo2 Constructor");
	}

	public void writeExternal(ObjectOutput out) throws IOException {
		System.out.println("Demo2 writeExternal()");
	}

	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		System.out.println("Demo2 readExternal()");
	}
}

public class ExternizationDemo {
	// Throw exceptions to console:
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		System.out.println("--- GOing to create objects ---");
		Demo1 b1 = new Demo1();
		Demo2 b2 = new Demo2();
		ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("Demo1.out"));
		System.out.println("--- Saving objects ---");
		o.writeObject(b1);
		o.writeObject(b2);
		o.close();

		// Get back objects
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("Demo1.out"));
		System.out.println(" --- Recovering b1 --- ");
		b1 = (Demo1) in.readObject();
		System.out.println(" --- Object recovered --- ");
		System.out.println(" --- Recovering b2 --- ");
		b2 = (Demo2)in.readObject();
	}
}

Output :

— GOing to create objects —
Demo1 Constructor
Demo2 Constructor
— Saving objects —
Demo1 writeExternal()
Demo2 writeExternal()
— Recovering b1 —
Demo1 Constructor
Demo1 readExternal()
— Object recovered —
— Recovering b2 —
Demo2 Constructor
Demo2 readExternal()

O/p of same program when class implements serializable interface.

— GOing to create objects —
Demo1 Constructor
Demo2 Constructor
— Saving objects —
— Recovering b1 —
— Object recovered —
— Recovering b2 —

As we can see, in case of serialization object is readed from ObjectInputStream whereas in case externalization, default no args constructor is called (Object is created).

Related posts

5 thoughts on “Explain Externalizable in Java”

  1. Hi,
    I am highly impressed with this blog in serialization. It provides us a good amount of information about this topic.

    Rabindra Singh

Leave a Reply

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