6

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.

Java Beginer
  • 321
  • 2
  • 16

5 Answers5

2
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.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

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

bNd
  • 7,512
  • 7
  • 39
  • 72
  • 1
    I.e. it has no other functionality, than to explicitly mark the class as Serializable. Same as Clonable. – Kayaman Jul 04 '13 at 12:29
0

Serializable is marker interface and marker interface does not have any methods but significance of it in method signature so that JVM can identify it.

shreyansh jogi
  • 2,082
  • 12
  • 20
0

java.io.Serializable it's what is called a marker interface. It doesn't declare any functionality, it just flags to other entities handling the implementing class that it's serialisable. For a good discussion about these subjects have a look at:

marker interface in java

Why Java needs Serializable interface?

Community
  • 1
  • 1
APinto
  • 76
  • 7
0

we can't say that an interface which doesn't have any method is a marker interface. because the word "Marker" itself signifies the meaning that " marking something". so i say, the interface(whatever may be it's contents) by implementing which if a class gains some extra or specialized behavior like allow object to store into a persistence storage(Serializable) OR allow an object to make it's duplicate or raplica(Cloneable) OR allow a user to implement only one method(like run()) instead of implementing nearly 4 t0 5 methods in a subclass in thread programming(Runnable) .

these are the specialized behavior which can be gained by an object when it implements those interfaces which are nothing but called as MARKER INTERFACE.

CONCLUSION

Marker interface may or may not contain methods...

it can also be called as tagged interface,dummy interface,empty interface....

You can also refer which I found itself from SO:

Why Java needs Serializable interface?

Community
  • 1
  • 1
Manish Doshi
  • 1,205
  • 1
  • 9
  • 17