Example – Externalizable Interface with inheritance

In the previous post we discussed about Externalizable Interface, how it works, how it is different from Serializable Interface and how to implement it. But what will happen if a class implementing the Externalizable interface also has a parent class. How to save the data of the parent class and how to recover it.

In serializable interface all this was done automatically as it saves the state of all the classes till the top of the heirarchy but this is not the case in Externalizable interface. In Externalizable interface we have to implement the readExternal() and writeExternal() methods in each and every class we wants to serialize or deserialize.

Whenever we need to store the data of super classes also we just call the respective method using the keyword “super” to ensure that the desired variables of the parent classes are also saved. Since we have already discussed about the Externalizable Interface in the previous post I would directly jump on the example here and show you how to serialize and deserialize in case of inheritence.

Example – Using Externalizable Interface with Inheritance

It is similar to the example of Externalizable interface but you just need to call methods of super class so as to read and write the data. But remember the order of reading and writing data should always be same, i.e. even during the super class method call if you call writeExternal first then you have to call readExternal also as the first method call. For reference see the example below.

1)  Vehicle.Java
package com.codingeek.externalizable;

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

public class Vehicle implements Externalizable {
	private String company;
	private String model;
	private String color;
	private int speed;

	/**
	 * This method writes the desired variables in the file.
	 */
	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		out.writeObject(company);
		out.writeObject(model);
		out.writeObject(color);
		out.writeInt(speed);
	}

	/**
	 * This methods read the variables from stream
	 */
	@Override
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		company = String.valueOf(in.readObject());
		model = String.valueOf(in.readObject());
		color = String.valueOf(in.readObject());
		speed = in.readInt();
	}
	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	@Override
	public String toString() {
		return "Vehicle [company=" + company + ", model=" + model + ", color="
				+ color + ", speed=" + speed + "]";
	}

}

2) Car.Java
package com.codingeek.externalizable;

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

public class Car extends Vehicle implements Externalizable {
	private Integer ccOfEngine;
	private String licenseNumber;
	private Integer numberOfDoors;

	public Integer getCcOfEngine() {
		return ccOfEngine;
	}

	public void setCcOfEngine(Integer ccOfEngine) {
		this.ccOfEngine = ccOfEngine;
	}

	public String getLicenseNumber() {
		return licenseNumber;
	}

	public void setLicenseNumber(String licenseNumber) {
		this.licenseNumber = licenseNumber;
	}

	public Integer getNumberOfDoors() {
		return numberOfDoors;
	}

	public void setNumberOfDoors(Integer numberOfDoors) {
		this.numberOfDoors = numberOfDoors;
	}

	@Override
	public String toString() {

		return super.toString() + "\n" + "Car [ccOfEngine=" + ccOfEngine
				+ ", licenseNumber=" + licenseNumber + ", numberOfDoors="
				+ numberOfDoors + "]";
	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		super.writeExternal(out);
		out.writeInt(ccOfEngine);
		out.writeObject(licenseNumber);
		out.writeInt(numberOfDoors);
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		super.readExternal(in);
		ccOfEngine = in.readInt();
		licenseNumber = String.valueOf(in.readObject());
		numberOfDoors = in.readInt();
	}
}

3)  SerializationUtil.Java
package com.codingeek.externalizable;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * This class is a utility class for performing the serialization and
 * deserialization operations provided the required information.
 */
public class SerializationUtil {

	/**
	 * deserialize to Object from given file. We use the general Object so as
	 * that it can work for any Java Class.
	 */
	public static Object deserialize(String fileName) throws IOException,
			ClassNotFoundException {
		FileInputStream fis = new FileInputStream(fileName);
		BufferedInputStream bis = new BufferedInputStream(fis);
		ObjectInputStream ois = new ObjectInputStream(bis);
		Object obj = ois.readObject();
		ois.close();
		return obj;
	}

	/**
	 * serialize the given object and save it to given file
	 */
	public static void serialize(Object obj, String fileName)
			throws IOException {

		FileOutputStream fos = new FileOutputStream(fileName);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		oos.writeObject(obj);
		oos.close();
	}
}

4)  SerializationTest.Java
package com.codingeek.externalizable;

import java.io.IOException;

public class SerializationTest {

	public static void main(String[] args) {
		Car car = new Car();
		car.setCcOfEngine(1000);
		car.setLicenseNumber("AW-23-6528");
		car.setNumberOfDoors(4);
		car.setColor("Black");
		car.setCompany("Mercedes");
		car.setModel("Mercedez- Benz -Q");
		try {
			/**
			 * Serializing the object
			 */
			SerializationUtil.serialize(car, "serialization.txt");

			/**
			 * Deserializing the object
			 */
			Car carOutput = (Car) SerializationUtil
					.deserialize("serialization.txt");
			System.out.println(carOutput.toString());

		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}

Don’t forget to share your thoughts in comment box and share the wisdom with your friends.
Keep Learning. Happy Learning. :)