1

The text is as follows:

If the object has a non-serializable class somewhere up its inheritance tree, the constructor for that non-serializable class will run along with any constructors above that (even if they're serializable). Once the constructor chaining begins, you can't stop it, which means all superclasses, beginning with the first non-serializable one, will reinitialize their state.

My question is , if a class is non-serializable, how can any of its super classes be serializable(thats what they are implying when they say "even if they're serializable"), because if they were serializable, then the "non-serializable" subclass in question should also be serializable.

Jens
  • 67,715
  • 15
  • 98
  • 113
user3760100
  • 679
  • 1
  • 9
  • 20

2 Answers2

1

That's true, the "(even if they're serializable)" is confusing. As you interpreted things correctly, serializability is declared by implements Serializable which is inherited just like any other interface. Therefore a Serializable class cannot have a non-Serializable subclass, at least by the definition of the interfaces. Therefore, the "with any constructors above that (even if they're serializable)" makes no sense.

A subclass of a Serializable can still prevent serialization by breaking things at runtime in multiple ways, but that doesn't make them not Serializable according to the technical definition of Serializable.

Christian Hujer
  • 17,035
  • 5
  • 40
  • 47
0

if they were serializable, then the "non-serializable" subclass in question should also be serializable.

No, that's wrong. You can write something like this:

class A implements Serializable {
...
}
class B extends A {
    private Object o;
    //voila! non-serializable class

}

You cannot add non-serializable field to class without breaking serialization. If you try to serialize/deserialize B objects, you'll get NotSerializableException.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Everv0id
  • 1,862
  • 3
  • 25
  • 47
  • The NotSerializableException is because class B has an instance variable Object o, which does not implement Serializable. This Exception can be overcome by changing private Object o to transient private Object o; If you do that, you no longer get the exception. You no longer get an exception because class B implicitly implements Serializable. Your answer is wrong. – user3760100 Dec 15 '14 at 12:01
  • 1
    `class B` can still be `Serializable` despite non-`Serializable` field `o` if it implements `readObject()` and `writeObject()` to provide handling for the non-transient fields. For example, it could be a monitor object used for `synchronized`. It's often not necessary to serialize it, so you would usually find `private Object lock;` and `private void readObject(ObjectInputStream in) { in.defaultReadObject(); lock = new Object(); }` and `private void writeObject(ObjectOutputStream out) { out.defaultWriteObject(); }`. – Christian Hujer Dec 15 '14 at 13:31
  • @user3760100 ofc if you remove non-serializable fields from child class you will not get an exception. But how it disprove my answer? I mean, classes inherited from serializable classes **can be** non-serializable. You *can* make them serializable using `transient` or `read/writeObject` implementation. – Everv0id Dec 15 '14 at 15:58
  • @Everv0id , your statement that classes inherited from serializable classes can be non-serializable is wrong. A sub class inherits all properties of its super class. If the super class has the property that it can be serialized, so does the sub class. – user3760100 Dec 15 '14 at 16:18
  • @user3760100 see [this link](http://stackoverflow.com/questions/13895867/java-io-notserializableexception). Without making non-serializable fields `transient` or implementing `readObject` and `writeObject`, you'll get `NotSerializableException` anyway. – Everv0id Dec 15 '14 at 16:49
  • @Everv0id just make a class A that implements Serializable, make a class B that extends A, but does not extend Serializable, create a class C that implements Serializable, create an instance variable of type B in class C and try to serialize objects of class C. They get serialized without any exceptions being thrown. This is possible, only if class B is Serializable.This proves that class B is serializable. Since class B does not implement Serializable, it's serializability can only be attributed to it extending class A which implements Serializable. – user3760100 Dec 16 '14 at 05:33
  • @Everv0id , the link provided by you is not official. Here is the official link provided that clearly states what I have stated previously in the first paragraph. Please have a look and let me know if I am wrong.https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html – user3760100 Dec 16 '14 at 06:01
  • @user3760100 If you try to do `readObject` or `writeObject` on object which doesn't implement `Serializable` or `Externalizable`, you'll get an exception anyway. If you doubt, see [this link](http://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html). – Everv0id Dec 16 '14 at 14:29