Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Marker interface in java - Serialization

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 266
    Comment on it

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

    Example:

    Student.java

    import java.io.Serializable;
     
    public class Student implements Serializable
    {
       public String firstName;
       public String lastName;
       
    }
    
    
    

    MainDemo.java

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
     
    public class MainDemo {
     
    public static void main(String[] args) {
    Student student = new Student();
    student.firstName = "Chris";
    student.lastName = "Gayle";
     
    try {
    FileOutputStream fout = new FileOutputStream("[file-name].txt");
    ObjectOutputStream out = new ObjectOutputStream(fout);
    out.writeObject(student);
    out.close();
    fout.close();
    System.out.print("Serialized data is saved in [file-name].txt file");
    } catch (IOException e) {
    
    }
    }
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: