Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialization Interface is empty in JDK Source. What is the use of implementing it [duplicate]

It was marked duplicate and i am expanding my question.

My question is how JDK internally serializing objects. How ObjectxxxStreams class serializing when the class implements that interface.?

I was looking into the serialization topic and deeply dived into the JDK Source code.

This was the source code of serialization Interface in JDK.

package java.io;
public interface Serializable {
}

There is nothing in this interface. What is the use of implementing this interface. I know that, to serialize a object we should implement this. I know what serialization is and how to work with that. But how serialization happens internally using ObjectInputStream and ObjectOutputStream. These classes are how related to serialization. Alternatively let us keep that those two classes are doing their duty. All my question is why we need to implement this empty interface to serialize and deserialize objects and how it works internally? Please explain in detail about this.

like image 625
Java Beginer Avatar asked Nov 12 '22 02:11

Java Beginer


2 Answers

why we need to implement this empty interface ??

Its a design pattern

The marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects. It provides a means to associate metadata with a class where the language does not have explicit support for such metadata.

like image 105
Suresh Atta Avatar answered Nov 14 '22 23:11

Suresh Atta


Serializable is marker interface

java.io.Serializable marker interface. It doesnot has any members defined it it. When a java class is to be serialized, you should intimate the java compiler in some way that there is a possibility of serializing this java class. In this scenario, marker interfaces are used.

Refer javadoc and Requirement of the serializable interface

like image 33
bNd Avatar answered Nov 14 '22 23:11

bNd