Externalizable Interface in Java

Just like Serializable Interface, Externalizable Interface  is also used to serialize and deserialize the object.

In serialization, it is always possible to save the total object to file, it is not possible to save a part of object, which may create performance problem. So, to overcome these problem Externalizable Interface was introduced.

In serialization, everything is taken care by JVM and programmer does not have any control over it. But in Externalizable Interface everything is taken care by programmer, and JVM does not have any control over it.

Based on our requirement we can save either the total object or part of the object which can improves the performance of the system.

To provide the externalizable ability for any java object, it is mandatory for the corresponding class to implement Externalizable interface.

It is not a Marker interface like Serializable Interface because it has two methods:

  • public void writeExternal(objectOutput out) throws IOException
  • public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException

User.java

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

public class User implements Externalizable{

    private String name;
    private int age;
    private int roleNo;

    public User(){}

    public User(String name, int age, int roleNo){
        this.name = name;
        this.age = age;
        this.roleNo = roleNo;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public void setRollNo(int roleNo) {
        this.roleNo = roleNo;
    }
    public int getRollNo() {
        return roleNo;
    }
    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject(name);
        out.writeInt(roleNo);
    }
    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        name = (String)in.readObject();
        roleNo = in.readInt();
    }
}

				
			

Main.java

				
					package externalizable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Main {
    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
        File f = new File("abc.txt");
        User u = new User("Satish", 25, 101);

        FileOutputStream fos = new FileOutputStream(f);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(u);
        System.out.println("User Object is serialized !!");
        System.out.println("==============");

        FileInputStream fis = new FileInputStream(f);
        ObjectInputStream ois = new ObjectInputStream(fis);
        User user = (User)ois.readObject();
        System.out.println("User objetc is deserialized !!");
        System.out.println("Name is : "+user.getName());
        System.out.println("Age is : "+user.getAge());
        System.out.println("RollNo is : "+user.getRollNo());
        System.out.println("==============");
    }
}

				
			

Output and Explanation:

				
					User Object is serialized !!
==============
User objetc is deserialized !!
Name is : Satish
Age is : 0
RollNo is : 101
==============

Here in the output we are getting Age as 0. It is because in the User.java class we are serializing only Name and RollNo and not the Age that is why we are getting Age as 0.
Hence If you want to serialize some part of any object the we should go for Externalizable Interface.
				
			
Scroll to Top