Why I don't have an error while I am trying to read Child class into Parent's class variable?
I thought that before asigning object that have been read compiler checks serialVersionUID
of both classes (variable class and class that have been read) and if serialVersionUID
is not equal InvalidClassException
is thrown. So does A
class and B
class have the same serialVersionUID
?
import java.io.*;
class A {}
class B extends A implements Serializable {}
public class Test
{
public static void main(String[] args) throws IOException, ClassNotFoundException
{
FileOutputStream fileOutputStream = new FileOutputStream("b.bin");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
B b1 = new B();
objectOutputStream.writeObject(b1);
objectOutputStream.close();
FileInputStream fileInputStream = new FileInputStream("b.bin");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
A a1 = (A) objectInputStream.readObject(); // why I don't have an InvalidClass Exception here
objectInputStream.close();
}
}