-
Serilazable interface
almost 9 years ago
-
over 8 years ago
It is a marker interface,and does not have any methods like Serializable, Clonneable etc.These marker interface indicate somethink to JVM or compiler.If the JVM sees Clonnable interface it performs some operation to support cloning. Same applies to the Serializable and other marker interfaces as well.
-
-
almost 9 years ago
Marker Interface in java:
Marker Interface is an interface in which there are no members. i.e we don't define any methods or data members inside the marker interface. It is also called tag interface.
A very obvious question comes in mind that if there no members defined inside this interface then what's the use of this interface? Here is the answer:
- This is used to give a particular signal to compiler/JVM to perform various operations based on the type of the marker interface defined.
- There can be other alternatives also but by using this one can avoid complexity the code is more organised and readable by using this interface.
- It serves as similar purpose as that of the annotations in java.
Example of Marker Interface:
Serializable interface in java:
Let us define serialization first:
Serialization:
It is a process in which the objects are converted in the form of byte streams. i.e object can be saved as stream of bytes.
Purpose: Byte stream is a form which is independent of the platforms and hence the code which is serialized in one platform can be used in other platform simply by deserializing the code for that particular platform.
It can be used for various purposes such as:Writing data to disk
Storing data in the memory
Byte stream can be stored in DB in the form of BLOB
To serve this purpose of serialization we have to use java.io.serializable interface:
It is one of the types of Marker Interfaces in java. Its role comes into play when a class needs to be serialized.- It is defined as java.io.Serializable.
- There may arise a need when your class needs to be serialized . So you should inform the compiler that there is possibility that a particular class may be serialized.
- So for serving this purpose that particular class has to implement Serializable marker interface. By implementing this, the particular class is marked as serializable.
- The subclass are by default serializable if the super class has already implemented java.io.Serializable interface.
- After java 1.5 the use of these interfaces have become obselete due to the introduction of annotations. It serves as a better and effective way of doing serialization as it provides various added functionality. It can be implemented by adding the @Serializable annotation before the class name which needs to be serialized.
-
-
almost 9 years ago
Marker interface in Java is interfaces with no field or methods or in simple word empty interface in java is called marker interface. Example of marker interface is Serializable, Clonnable and Remote interface.
Serialization in java is a mechanism of writing the state of an object into a byte stream.
java.io.Serializable interface
Serializable is a marker interface (has no body). It is just used to "mark" java classes which support a certain capability.
It must be implemented by the class whose object you want to persist.
Example:
public class Student implements Serializable{
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
}ObjectOutputStream class
The ObjectOutputStream class is used to write primitive data types and Java objects to an OutputStream. Only objects that support the java.io.Serializable interface can be written to streams.
-
3 Answer(s)