0

What is the use of implementing Serializable interface? It is said that Serializable is a marker interface. A marker interface provides run-time information to JVM. What wonders me what is happening in the runtime? How OutputObjectStream is able to serialize an object which implements Serializable?

somesh
  • 63
  • 7
  • 2
    What makes you think that this interface is only relevant to the COMPILER? The compiler simply adds "this class implements Serializable" to the class file when compiling a class. Later, at runtime, when you have code that tries to serialize object some object X of class Y, then it becomes relevant whether Y implements Serializable or not. – GhostCat May 06 '22 at 10:50
  • In other words: the real answer here is for you to step back and to research how serialization works overall, as "end to end" feature. – GhostCat May 06 '22 at 10:51
  • Does this answer your question? [What does Serializable mean?](https://stackoverflow.com/questions/3429921/what-does-serializable-mean) – f_puras May 06 '22 at 11:08
  • Thanks. I've found that i've wrongly mentioned compiler. I want to know how OutputObjectStream is able to serialize objects implementing serializable interface and not the objects from class which doesn't implement Serializable. – somesh May 06 '22 at 11:10
  • 1
    It can't, because serialization is only allowed if the class is `Serializable`. That is it, there is nothing more to it. Serialization comes with additional requirements (every fields needs to be serializable or otherwise marked as transient, there are special concerns for changing your class, etc). Implementing Serializable signals that you are aware of this, and accept the consequences (it also avoids some security issues that could exist if everything was serializable by default). – Mark Rotteveel May 06 '22 at 11:12

0 Answers0