Java: Object Externalizable Serialization with Inheritance Example

Serialization in the inheritance with the implementation of Externalizable interface can be done in two ways:

  • Implements Externalizable on the parent class
  • Implements Externalizable on Child class

In these below examples consider both the cases and implementation ways for each.

Pre-requisite:

Example: Serialization in Inheritance where Externalizable interface implements on Parent class Only

Here Externalizable interface implements on parent class and child class also. That’s what override writeExernal() and readExternal() methods on parent and child classes to serialize fields of individual classes.

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

public class Person implements Externalizable{
	private String name;
	private String citizenship;

	public Person()
	{
	}

	public Person(String name, String citizenship) {
		super();
		this.name = name;
		this.citizenship = citizenship;
	}

	public String getName() {
		return name;
	}

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

	public String getCitizenship() {
		return citizenship;
	}

	public void setCitizenship(String citizenship) {
		this.citizenship = citizenship;
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		// own class fields deserialization
		name=(String) in.readObject();
		citizenship=(String) in.readObject();
	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		// own class fields serialization
		out.writeObject(name);
		out.writeObject(citizenship);
	}

}

Employee class serializing fields of the self class and calling super.writeExternal() and super.readExternal() to serialize and deserialize fields of the parent class.

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

public class Employee extends Person implements Externalizable {
	private int employeeId;
	private String department;
	private int salary;

	public Employee() {

	}

	public Employee(int employeeId, String name, String department, String citizen, int salary) {
		super(name, citizen);
		this.employeeId = employeeId;
		this.department = department;
		this.salary = salary;
		System.out.println("Employee Constructor Executed.");
	}

	public int getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(int employeeId) {
		this.employeeId = employeeId;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public int getSalary() {
		return salary;
	}

	public void setSalary(int salary) {
		this.salary = salary;
	}

	/**
	 * Since super class person implemented Externalizable interface then call super.writeExternal() for Serializing the parent class fields.
	 */
	@Override
	public void writeExternal(ObjectOutput out) throws IOException {

		//person superclass fields
		super.writeExternal(out);
		// Employee class fields serilization
		out.writeInt(employeeId);
		out.writeObject(department);
		out.writeInt(salary);
	}

	/**
	 * Since super class person implemented Externalizable interface then call super.readExternal() for Deserializing the parent class fields.
	 * here
	 */
	@Override
	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		// Person superclass fields deserilization
		super.readExternal(in);
		//Employee class fields deserilization
		employeeId = in.readInt();
		department = (String) in.readObject();
		salary =  in.readInt();

	}

}

Here are steps to implements serialization and deserialization of Employee class.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializationAndDeserializationInheritanceExample {

	public static void main(String[] args) throws Exception {
		// creating employee object
		Employee e1 = new Employee(111, "Saurabh","IT","India", 52000);

		//serialization of object
		serializeEmployee(e1, "employee.txt");

		//deserialization of object
		e1 = deserializeEmployee("employee.txt");
		System.out.println("After Deserilization Employee Detail :");
		System.out.println("Id :" + e1.getEmployeeId() + "\nName: " + e1.getName() + "\nDepartment: " + e1.getDepartment()+ "\nSalary: " + e1.getSalary()+ "\nCitizenship: " + e1.getCitizenship());
	}

	private static void serializeEmployee(Employee e1, String fileName) throws Exception {
		// writing object into employee file
		FileOutputStream f = new FileOutputStream(fileName);
		ObjectOutputStream out = new ObjectOutputStream(f);
		// Serialize employee object as stream and write in file
		out.writeObject(e1);
		out.flush();
		out.close();
		f.close();
		System.out.println("Employee object Serialize Successfully");
	}

	private static Employee deserializeEmployee(String fileName) throws Exception {
		// Read object stream from file
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
		//Deserialize object stream to employee object
		Employee e = (Employee) in.readObject();
		in.close();
		return e;
	}
}

After executing the above program you will get below result.

Output


Employee Constructor Executed.
Employee object Serialize Successfully
After Deserilization Employee Detail :
Id :111
Name: Saurabh
Department: IT
Salary: 52000
Citizenship: India

Example: Serialization in Inheritance where Externalizable interface implements on Child class Only

Here Externalizable interface implements on parent class and child class also. That’s what override writeExernal() and readExternal() methods on parent and child classes to serialize fields of individual classes.

public class Person {
	private String name;
	private String citizenship;

	public Person()
	{
	}

	public Person(String name, String citizenship) {
		super();
		this.name = name;
		this.citizenship = citizenship;
	}
    //getter and setter of class
}
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

public class Employee extends Person implements Externalizable {
	private int employeeId;
	private String department;
	private int salary;

	public Employee() {

	}

	public Employee(int employeeId, String name, String department, String citizen, int salary) {
		super(name, citizen);
		this.employeeId = employeeId;
		this.department = department;
		this.salary = salary;
		System.out.println("Employee Constructor Executed.");
	}

	//getter and setter of class

	/**
	 * Since super class person not implemented Externalizable interface then for Serializing the parent class fields
	 * we can do it here also.
	 */
	@Override
	public void writeExternal(ObjectOutput out) throws IOException {

		//person superclass fields
		out.writeObject(getName());
		out.writeObject(getCitizenship());

		// Employee class fields serilization
		out.writeInt(employeeId);
		out.writeObject(department);
		out.writeInt(salary);
	}

	/**
	 * Since super class person not implemented Externalizable interface then for deserialize the parent class fields
	 * here
	 */
	@Override
	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		// Person superclass fields deserilization
		setName((String) in.readObject());
		setCitizenship((String) in.readObject());

		//Employee class fields deserilization
		employeeId = in.readInt();
		department = (String) in.readObject();
		salary =  in.readInt();

	}

}

Here are steps to implements serialization and deserialization of Employee class.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializationAndDeserializationInheritanceExample {

	public static void main(String[] args) throws Exception {
		// creating employee object
		Employee e1 = new Employee(111, "Saurabh","IT","India", 52000);

		//serialization of object
		serializeEmployee(e1, "employee.txt");

		//deserialization of object
		e1 = deserializeEmployee("employee.txt");
		System.out.println("After Deserilization Employee Detail :");
		System.out.println("Id :" + e1.getEmployeeId() + "\nName: " + e1.getName() + "\nDepartment: " + e1.getDepartment()+ "\nSalary: " + e1.getSalary()+ "\nCitizenship: " + e1.getCitizenship());
	}

	private static void serializeEmployee(Employee e1, String fileName) throws Exception {
		// writing object into employee file
		FileOutputStream f = new FileOutputStream(fileName);
		ObjectOutputStream out = new ObjectOutputStream(f);
		// Serialize employee object as stream and write in file
		out.writeObject(e1);
		out.flush();
		out.close();
		f.close();
		System.out.println("Employee object Serialize Successfully");
	}

	private static Employee deserializeEmployee(String fileName) throws Exception {
		// Read object stream from file
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
		//Deserialize object stream to employee object
		Employee e = (Employee) in.readObject();
		in.close();
		return e;
	}
}

After executing the above program you will get below result.

Output


Employee Constructor Executed.
Employee object Serialize Successfully
After Deserilization Employee Detail :
Id :111
Name: Saurabh
Department: IT
Salary: 52000
Citizenship: India

See Also: