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.
Asked
Active
Viewed 3,259 times
0
-
2Why shouldn't they? Surely it makes sense for these classes to implement `Serializable` rather 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. – Michael Berry Feb 04 '14 at 14:53
-
'So many' is meaningless. * Which* JDK classes implement Serializable that shouldn't, in your opinion? – user207421 Feb 04 '14 at 17:38
3 Answers
3
Because there are cases which require the instances of these classes to be

Community
- 1
- 1

Konstantin Yovkov
- 62,134
- 8
- 100
- 147
3
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

Philipp Sander
- 10,139
- 6
- 45
- 78
-
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 – kushi Feb 04 '14 at 15:37
-
1yes! if an attribute type doesn't implement serializable, it cannot be serialized – Philipp Sander Feb 04 '14 at 15:43
-
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?? – kushi Feb 04 '14 at 15:57
-
you may read this http://stackoverflow.com/questions/441196/why-java-needs-serializable-interface – Philipp Sander Feb 04 '14 at 15:59
2
Because these classes are meant to be stored in some persistent storage or transferred via network as stream of bytes.

Aniket Thakur
- 66,731
- 38
- 279
- 289