please can someone explain why the constructor of the class ‘Gambler’ is called after deserialization but say the constructor of the class ‘Player’ is not?
import java.io.*;
class Gambler {
Gambler() { System.out.print("d"); }
}
class Person extends Gambler implements Serializable {
Person() { System.out.print("c"); }
}
class Player extends Person {
Player() { System.out.print("p"); }
}
class CardPlayer extends Player implements Serializable {
CardPlayer() { System.out.print("c"); }
public static void main(String[] args) {
CardPlayer c1 = new CardPlayer();
try {
FileOutputStream fos = new FileOutputStream("play.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(c1);
os.close();
FileInputStream fis = new FileInputStream("play.txt");
ObjectInputStream is = new ObjectInputStream(fis);
CardPlayer c2 = (CardPlayer) is.readObject();
is.close();
}
catch (Exception x ) { }
}
}
Because the
Gamblerclass does not implement theSerializableinterface. From javadocs ofSerializable:See the
java.io.Serializablejava docs.