Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the penalty for unnecessarily implementing Serializable?

I need to develop a Java RMI application for my distributed systems class.

During the lecture, the professor was stressing to only let classes implement Serializable that have to be passed by value over the network.

This implies that there is some downside or penalty to letting too many classes implement Serializable. Classes that don't require to be sent over the network.

I don't see how there could be any downside since the serialization/deserialization would never happen if you never actually send it over the network.

like image 668
Thomas Vanhelden Avatar asked Aug 22 '16 12:08

Thomas Vanhelden


People also ask

What happens if we do not 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.

What happens if the object to be serialized?

To serialize an object means to convert its state to a byte stream so way that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java.

What are the disadvantages of Serializable?

Disadvantages of Serialization in JavaSometimes the byte streams do not convert into objects completely which leads to errors.


2 Answers

only let classes implement Serializable that have to be passed by value over the network.

Your professor is suggesting you minimize your use of Serializable to areas where it's strictly needed.

This is because serialization is a strong candidate for leaking implementation. Implementing Serializable shows intent of serialization (even if the object is never actually serialized), which imposes the idea that developers should take caution when modifying those classes to avoid breaking software.


Joshua Bloch covers this in his book Effective Java.

The moment you serialize an object, the class that it was instantiated from can no longer be modified without special treatment. If you modify the class, the binary representation will no longer match the objects already serialized. Thus deserialization of any objects serialized before modifying the class will fail.

If a type implements Serializable, it has the potential to be serialized. If an instance of that type was serialized, you may break code by modifying it's implementation.

Since there's no easy way of knowing for sure that an instance of a serializable type has been serialized (albeit you may not intend for objects to be serialized), developers take strong caution when modifying implementations of those types.


- This could be avoided by properly versioning your serializable types, but due to the potential of versioning a type that had no contract change (with no compile time error handling support to notify you), it's best to keep explicit versioning minimal to avoid adding excess complexity to your design.

like image 197
Vince Avatar answered Sep 22 '22 22:09

Vince


What is the penalty for unnecessarily implementing Serializable?

There is no penalty for unnecessarily implementing Serializable. If you never serialize the object, nothing happens just because you added implements Serializable. If you do serialize it, it works instead of failing. That's not a penalty.

Why the professor was stressing that is a mystery. Ask him. There is no overhead other than when serializing, and if you're passing objects by value via RMI you don't have any choice but to implement Serializable, so there is nothing to evalute the overhead against. It is meaningless.

like image 37
user207421 Avatar answered Sep 23 '22 22:09

user207421