Why serializable interface is used in Java?
Why serializable interface is used in Java?
Serializable is a marker interface (has no data member and method). It is used to “mark” Java classes so that the objects of these classes may get a certain capability. The Cloneable and Remote are also marker interfaces. The Serializable interface must be implemented by the class whose object needs to be persisted.
What is serialization in Java and why it is used?
Well, serialization allows us to convert the state of an object into a byte stream, which then can be saved into a file on the local disk or sent over the network to any other machine. And deserialization allows us to reverse the process, which means reconverting the serialized byte stream to an object again.
Which interface is used in serialization in Java?
Java Serializable interface
The Java Serializable interface ( java. io. Serializable is a marker interface your classes must implement if they are to be serialized and deserialized. Java object serialization (writing) is done with the ObjectOutputStream and deserialization (reading) is done with the ObjectInputStream.
How does serializable interface work?
Serializable interface. The Serializable interface is a marker interface; it declares no methods at all. It tells the serialization mechanism that the class can be serialized. That is done by calling the writeObject() method of the java.
What is the use of clonable,and Serializable interface?
A serializable interface is used to persist an object. The cloneable interface is used to clone the class objects. Both these interfaces are marker interfaces i.e. they are empty. But when a class implements them, then they indicate that the compiler can expect some special behavior from the classes implementing them.
What interface is used in Java?
Serializable interface : Serializable interface is present in java.io package. It is used to make an object eligible for saving its state into a file. This is called Serialization. Classes that do not implement this interface will not have any of their state serialized or deserialized.
Can we create an instance of an interface in Java?
You cannot create an instance of an interface, because an interface is basically an abstract class without the restriction against multiple inheritance. And an abstract class is missing parts of its implementation, or is explicitly marked as abstract to prohibit instantiation.
Why do we need interfaces in Java?
Why we need Interface in Java. There are several reasons, an application developer needs an interface, one of them is Java’s feature to provide multiple inheritance at interface level. It allows you to write flexible code, which can adapt to handle future requirements.