Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why classes designed for inheritance should rarely implement Serializble interface?

According to Effective Java :

Classes designed for inheritance should rarely implement Serializable, and interfaces should rarely extend it.

What is the reason behind this statement? In other words what would be the problems if they do implement Serializable interface?

like image 431
Geek Avatar asked Jun 05 '13 17:06

Geek


People also ask

Can we serialize class without implementing Serializable interface?

If the superclass is Serializable, then by default, every subclass is serializable. Hence, even though subclass doesn't implement Serializable interface( and if its superclass implements Serializable), then we can serialize subclass object.

What will happen if we don't implement Serializable?

The Student would not be Serializable, and it will act like a normal class. Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link.

What happens if a class implements Serializable?

If a super class implements Serializable, then its sub classes do automatically. When an instance of a serializable class is deserialized, the constructor doesn't run. If a super class doesn't implement Serializable, then when a subclass object is deserialized, the super class constructor will run.

Why do we need to implement Serializable interface?

Serializable is a marker interface (has no data member and method). It is used to "mark" Java classes so that the objects of these classes may get a certain capability. The Cloneable and Remote are also marker interfaces. The Serializable interface must be implemented by the class whose object needs to be persisted.


2 Answers

The next sentence in the same item says it:

Classes designed for inheritance (Item 17) should rarely implement Serializable, and interfaces should rarely extend it. Violating this rule places a significant burden on anyone who extends the class or implements the interface. There are times when it is appropriate to violate the rule. For example, if a class or interface exists primarily to participate in a framework that requires all participants to implement Serializable, then it makes perfect sense for the class or interface to implement or extend Serializable.

Since I do not want Josh to be on my tail for infringing the copyright (as cool as that might be), I wouldn't copy all the item in this answer. Suffice to say the the reasoning for this is explained in the rest of the item.

EDIT: Josh listed a number of costs for implementing Serializable. If an interface / superclass implements it, the costs will be forced onto the extending classes.

A major cost of implementing Serializable is that it decreases the flexibility to change a class’s implementation once it has been released. ...

...

A second cost of implementing Serializable is that it increases the likelihood of bugs and security holes. ...

A third cost of implementing Serializable is that it increases the testing burden associated with releasing a new version of a class.

like image 155
zw324 Avatar answered Sep 28 '22 11:09

zw324


If a base class or interface implements Serializable, it forces every subclass or implementation, to fulfill the contract of the superclass or interface, to make sure that the subclass or implementation is also serializable.

That prevents any implementation to add fields that are non transient and not serializable to their implementation, for example.

like image 32
JB Nizet Avatar answered Sep 28 '22 11:09

JB Nizet