I am learning the Serializable and Externalizable interface, and I see that when the Externalizable object is restored, the instance is created first using the open no-arg constructor, then the readExternal method is called. If the object does not support Externalizable, Serializable objects are restored by reading them from ObjectInputStream.
I donβt understand why we use ObjectInputStream for Appearance if the object cannot be read from there? What exactly is read from an ObjectInputStream? I think we are reading something from there if we use it.
I also found this diagram about deserializing an Externalizable or Serializable interface

What is the difference between Serializable and Externalizable during deserialization?
, Externalizable , ObjectInputStream , Serializable?
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("employee.ser"))
, FileInputStream , . ObjectInputStream , .
.
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
public class Employee implements Externalizable {
private int id;
private String name;
private int age;
public void Employee() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";
}
public void writeExternal(ObjectOutput oo) throws IOException {
System.out.println("Inside writeExternal method");
oo.writeInt(id);
oo.writeObject(name);
}
public void readExternal(ObjectInput oi) throws IOException, ClassNotFoundException {
System.out.println("Inside readExternal method");
id = oi.readInt();
name = (String) oi.readObject();
}
}
ExternalizableWrite
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class ExternalizableWrite {
public static void main(String args[]) {
ExternalizableWrite ew = new ExternalizableWrite();
ew.writeEmployeeObject();
}
private void writeEmployeeObject() {
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("employee.ser"))) {
Employee employee = new Employee();
employee.setId(101);
employee.setName("Peter");
employee.setAge(25);
System.out.println(employee);
oos.writeObject(employee);
System.out.println("Successfully written employee object to the file.\n");
} catch (FileNotFoundException ex) {
System.out.printf("ERROR: %s", ex);
} catch (IOException ex) {
System.out.printf("ERROR: %s", ex);
}
}
}
ExternalizableRead
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class ExternalizableRead {
public static void main(String args[]) {
ExternalizableRead er = new ExternalizableRead();
er.readEmployeeObject();
}
private void readEmployeeObject() {
try (ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("employee.ser"))) {
Employee employee = (Employee) ois.readObject();
System.out.println(employee);
System.out.println("Successfully read employee objecct to the file.\n");
} catch (FileNotFoundException ex) {
System.out.printf("ERROR: %s", ex);
} catch (IOException | ClassNotFoundException ex) {
System.out.printf("ERROR: %s", ex);
}
}
}