Hi If a class cannot implement serializable interface and if we try to serialize it, we should get a NotSerializableException.
Here I am not getting it. The Cat
class doesn't implement Serializable
and I try to serialize it. Compiled and run fine why?
import java.io.*;
class Cat{}
class MyTest{
public static void main(String a[]){
Cat c = new Cat();
try{
FileOutputStream fos = new FileOutputStream("test.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(c);
oos.close();
fos.close();
}
catch(Exception e){e.getMessage();}
try{
FileInputStream fis = new FileInputStream("test.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
c = (Cat)ois.readObject();
ois.close();
fis.close();
}
catch(Exception e){e.getMessage();}
}
}