If the class represents a model object that may be transferred across
the n/w, it should be serializable.
Is this reasoning correct?
This reasoning is somewhat correct. If you are creating a public API where compatibility will be important, you may wish to make classes serializable which might one day need to be serialized. However, if you are creating a private application, classes should be made serializable only when needed. One reason (among a few) to attempt to avoid implementing Serializable
is that it introduces the need to maintain and secure the newly added 'interface' to your serializable class: its byte-code representation.
If so, what is the logic behind some of native Java API classes being
serializable while others are not? From this list of native classes
that implement Serilizable, it appears that there is more than one
reason to implement serializable.
One of the main reasons why many classes in the Java API do not implement Serializable
is that, due to the nature of interfaces and inheritence, any class which extends a class which implements Serializable
must also implement Serializable
. For example, if java.lang.Object
implemented Serializable
, every java class would need to be designed to be properly serializable. This would place a large burden on the design of any class: suddenly, security and byte-code validity and compatibility would need to be considered.