I keep reading about serialization.. I understand how to serialize and deserialize custom objects. But I am not able to understand the rational behind why many classes in JAVA API implement Serialize by default.
3 Answers 3
Because there are cases which require the instances of these classes to be
answered Feb 4, 2014 at 14:50
Konstantin Yovkov
63k9 gold badges107 silver badges152 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Because you can only serialize objects that are serializable. So if you have a field of a non serializable type, this field will not be serialized
answered Feb 4, 2014 at 14:50
Philipp Sander
10.3k7 gold badges50 silver badges80 bronze badges
4 Comments
kushi
Do you meant to say a class having HAS-A relationship with a String field scenario? Ex An Emp class implementing Serializable having a field EmpName as String.When Emp is Serialized,EmpName will be serialized only because String implements Serializable
Philipp Sander
yes! if an attribute type doesn't implement serializable, it cannot be serialized
kushi
In that case,Object class can implement Serializable so that all objects in both API and custom classes will be automatically eligible for Serialization ryt??
Philipp Sander
you may read this stackoverflow.com/questions/441196/…
Because these classes are meant to be stored in some persistent storage or transferred via network as stream of bytes.
answered Feb 4, 2014 at 14:51
Aniket Thakur
69.5k43 gold badges290 silver badges299 bronze badges
Comments
lang-java
Serializablerather than having to write all the logic manually everytime you want to serialize an object? It doesn't force you to serialize them, it just makes it easy to if you need to.