Why not serialize abstract classes in Java?

I read that in general abstract classes should not be made Serializable in Java. Subclasses must be serializable (with custom methods for reading, writing, if necessary, for example, when abstract classes have fields).

What is the reason for this? Why is this considered a bad design?

Update1: I have an abstract class with some fields and three subclasses. At the moment I am using the following approach.

I made all subclasses serializable with custom read, write methods. In an abstract class, I have the following methods.

void writeFields(ObjectOutputStream out)throws IOException { .... } void readFields(ObjectInputStream in) throws IOException, ClassNotFoundException{ ... } 

In custom reading methods, writing to subclasses, I call these methods to (de) serialize fields in an abstract class. Is this approach right? Or is there another better approach?

Update 2: I took Tom's advice and made my abstract class Serializable. (I want all subclasses to be Serializable, and I have data in an abstract class). It's aloof, but to complete the story, I use reflection to change the final field, as Jeremy Manson advised.

+6
java oop serialization abstract-class
source share
4 answers

I do not know that this is necessarily a bad design. Serialization is an implementation issue (mind you, Josh Bloch disagrees with me), so it makes no sense for interfaces. If an abstract class has state, then you would like to make it serializable. If he has no condition, there really is no reason to create it.

Take an example. java.security.cert.Certificate is an abstract serializable class with a "type" serialization field. If it was not sequential, it would be impossible to serialize the subclass and set this field. You would be forced to hack.

Please note that java.io.Serializable is a hack. This should not have been an interface. Annotations (or evolution of a language such as transient ) would be more appropriate.

As always, it's best to prefer composition over inheritance rather than making a serializable serializable class.

+1
source share

I think the reason is that if the Abstract class implements Serializable, it cannot be said that the subtype should NOT be Serializable. Better to allow each specific type ...

+5
source share

Take an oposite position. If you want to de-serialize an object, what will its type be?

By definition, an abstract class cannot be created. If you can serialize it, that means it can also be deserialized, and this will give you an instance of an abstract class. This contradicts the definition of an abstract class and therefore cannot be done.

+3
source share

This is just a bad design because it is a forced solution, which is if you want the subclass to have non-serializable elements.

That's why.

eg. The list is not Serializable, but each implementation of the main list. (I know that a list is an interface, but an abstract class without members = / = interface)

0
source share

All Articles