34

Say you have these two classes, Foo and Bar where Bar extends Foo and implements Serializable

class Foo {

public String name;

public Foo() {
    this.name = "Default";
}

public Foo(String name) {
    this.name = name;
}
}

class Bar extends Foo implements java.io.Serializable {

public int id;

public Bar(String name, int id) {
    super(name);
    this.id = id;
}
}

Notice that Foo doesn't implement Serializable. So what happens when bar is serialized?

    public static void main(String[] args) throws Exception {

    FileOutputStream fStream=new FileOutputStream("objects.dat");
    ObjectOutputStream oStream=new ObjectOutputStream(fStream);
    Bar bar=new Bar("myName",21);
    oStream.writeObject(bar);

    FileInputStream ifstream = new FileInputStream("objects.dat");
    ObjectInputStream istream = new ObjectInputStream(ifstream);
    Bar bar1 = (Bar) istream.readObject();
    System.out.println(bar1.name + "   " + bar1.id);

} 

it prints "Default 21". The question is, why the default constructor get called when the class is not serialized?

Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
  • 1
    You can't suddenly create instances of an innocent class without calling its constructor, so the serial spec requires calling a constructor of non-serialisable classes. / You might want a serial proxy. – Tom Hawtin - tackline Dec 26 '11 at 00:45

3 Answers3

24

Serializable is just a "marker interface" for a given class.

But that class must adhere to certain rules:

http://docs.oracle.com/javase/1.5.0/docs/api/java/io/Serializable.html

To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case.

to answer @Sleiman Jneidi question asked in comment, in oracle documentation mentioned above, its clearly mentioned

During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream.

Thus, default no-arg constructor of class Foo called of, resulted in initialization.

dextermini
  • 382
  • 3
  • 12
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I know that. but why it calls the default constructor.I am not asking what is Serializable ? – Sleiman Jneidi Dec 25 '11 at 23:35
  • 5
    @sleimanjneidi because "To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility **for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields**". This means `Bar` should set `Foo`'s `name` field "by hand", as `Foo` is not serializable – fge Dec 25 '11 at 23:38
4

it may be that the defaultWriteObject can only write the non-static and non-transient fields of the current class. Once the superclass does not implements the Serializable interface, the fields in the superclass can not be serialized into the stream.

andy
  • 3,951
  • 9
  • 29
  • 40
  • Yes if superclass does not implement Serializable, it will not be serialized, so when deserialized,its default constructor will be called and default values will be allocated to all of its variables. – Akash5288 Jan 12 '14 at 18:26
0

Actually when you will read the parent class object back as it's not serialize at all.. so for the non serialize things again JVM go through the same process as it use to go when we create the new object using new keyword.

Vishesh
  • 21
  • 4