Externalization in Java

Serialization and Externalization are the same process but have extra functions and features. But both jobs are same.
Externalizable interface have two unimplemented methods.
To write an object:
public void writeExternal(ObjectOutput in) throws IOException;


To read an Object stream:
public void readExternal(ObjectInput out) throws IOException,
ClassNotFoundException;
Externalization is nothing but serialization but an alternative for it and Externalizable interface extends Serializable interface.
Externalization Object:
package com.java.bean;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

public class ExternalizationBean implements Externalizable{
	private String name;
	private int id;
	private transient String temp;
	 
	@Override
	public void readExternal(ObjectInput out) throws IOException,
			ClassNotFoundException {
		name = (String) out.readObject();	
		id=(int)out.readInt();		
		temp=(String) out.readObject();
		
	}

	@Override
	public void writeExternal(ObjectOutput in) throws IOException {
		in.writeObject(name);
		in.writeInt(id);
		in.writeObject(temp);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTemp() {
		return temp;
	}

	public void setTemp(String temp) {
		this.temp = temp;
	}
		
}
Will try to write an externalize the object into file:
package com.java.core;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import com.java.bean.ExternalizationBean;

public class ExternalizationTest {
	
	public static void main(String args[]) throws IOException, ClassNotFoundException{
		ExternalizationBean test=new ExternalizationBean();
	
		FileInputStream fis = new FileInputStream("external.txt");
		ObjectInputStream ois = new ObjectInputStream(fis);
		test.readExternal(ois);
	}
}
Will try to De-externalize the object:
package com.java.core;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import com.java.bean.ExternalizationBean;

public class DeExternalizationTest {
	
	public static void main(String args[]) throws IOException, ClassNotFoundException{
		ExternalizationBean test=new ExternalizationBean();
				
		FileInputStream fis = new FileInputStream("external.txt");
		ObjectInputStream ois = new ObjectInputStream(fis);
		test.readExternal(ois);
		
		System.out.println("*****name***********"+test.getName());
		
		System.out.println("********id********"+test.getId());
		
		System.out.println("********temp********"+test.getTemp());
	}
}
Output:
*****name***********java
********id********1
********temp********TEMP

Difference between Serialization and Externalization

  • In case of Serializable, default serialization process is used. while in case of Externalizable custom Serialization process is used which is implemented by application.Externalizing gives control over the read/write process by implementing the readExternal and writeExternal methods of Externalizable.
  • Serialization needs serialVersionUID, But Exteralization no need of serialVersionUID
  • Serializable object will have object with data, but Externalizable object will have only data. That means in serialization will store directly object. But in terms of externalization we have to store the Data which have an object.
  • erializable subclass can be serializable but externalizable object subclass can't be externalizable.