A code is worth a thousand nonsense:
public class Person { private String name; Person() { System.out.println("Person constructor is called."); } ... } public class Student extends Person implements Serializable { private static final long serialVersionUID = 8306482247141552618L; public Student(String name, long ID) { super(name); this.ID = ID; } ... }
The code shows that Person class is not implement Serializable whereas Student class did. By the time during the deserialization happened to Student object, the default constructor of Person class is called. Thus "Person constructor is called" will output in the console.
FileInputStream inputFile = new FileInputStream("saveObject"); ObjectInputStream inputStream = new ObjectInputStream(inputFile); Student student = (Student) inputStream.readObject();
No comments:
Post a Comment