Externalizable interface in Java

11 Jan, 2022
Externalization serves the purpose of custom Serialization, where we can decide what to store in stream.Externalizable interface present in java.io, is used for Externalization which extends Serializable interface. It consist of two methods which we have to override to write/read object into/from stream which are-  
// to read object from stream
void readExternal(ObjectInput in) 

// to write object into stream
void writeExternal(ObjectOutput out) 
Key differences between Serializable and Externalizable  
Implementation : Unlike Serializable interface which will serialize the variables in object with just by implementing interface, here we have to explicitly mention what fields or variables you want to serialize.
Methods : Serializable is marker interface without any methods. Externalizable interface contains two methods: writeExternal() and readExternal().
Process: Default Serialization process will take place for classes implementing Serializable interface. Programmer defined Serialization process for classes implementing Externalizable interface.
Backward Compatibility and Control: If you have to support multiple versions, you can have full control with Externalizable interface. You can support different versions of your object. If you implement Externalizable, it’s your responsibility to serialize super class.
public No-arg constructor: Serializable uses reflection to construct object and does not require no arg constructor. But Externalizable requires public no-arg constructor.
Below is the example for Externalization- 
Java
// Java program to demonstrate working of Externalization// interfaceimport java.io.*;class Car implements Externalizable {    static int age;    String name;    int year;     public Car()    {        System.out.println("Default Constructor called");    }     Car(String n, int y)    {        this.name = n;        this.year = y;        age = 10;    }     @Override    public void writeExternal(ObjectOutput out)        throws IOException    {        out.writeObject(name);        out.writeInt(age);        out.writeInt(year);    }     @Override    public void readExternal(ObjectInput in)        throws IOException, ClassNotFoundException    {        name = (String)in.readObject();        year = in.readInt();        age = in.readInt();    }     @Override public String toString()    {        return ("Name: " + name + "\n"                + "Year: " + year + "\n"                + "Age: " + age);    }} public class ExternExample {    public static void main(String[] args)    {        Car car = new Car("Shubham", 1995);        Car newcar = null;         // Serialize the car        try {            FileOutputStream fo                = new FileOutputStream("gfg.txt");            ObjectOutputStream so                = new ObjectOutputStream(fo);            so.writeObject(car);            so.flush();        }        catch (Exception e) {            System.out.println(e);        }         // Deserialization the car        try {            FileInputStream fi                = new FileInputStream("gfg.txt");            ObjectInputStream si                = new ObjectInputStream(fi);            newcar = (Car)si.readObject();        }        catch (Exception e) {            System.out.println(e);        }         System.out.println("The original car is:\n" + car);        System.out.println("The new car is:\n" + newcar);    }}
Output:  
Default Constructor called
The original car is:
Name: Shubham
Year: 1995
Age: 10
The new car is:
Name: Shubham
Year: 1995
Age: 10
In the example, the class Car has two methods- writeExternal and readExternal. So, when we write “Car” object to OutputStream, writeExternal method is called to persist the data. The same applies to the readExternal method. When an Externalizable object is reconstructed, an instance is created first using the public no-argument constructor, then the readExternal method is called. So, it is mandatory to provide a no-argument constructor. When an object implements Serializable interface, is serialized or deserialized, no constructor of object is called and hence any initialization which is implemented in constructor can’t be done. 
This article is contributed by Shubham Juneja. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Rishav Shandilya 2
vineethmaller
adnanirshad158
java-interfaces
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java
Collections in Java
Singleton Class in Java
Multidimensional Arrays in Java
Stack Class in Java
Set in Java