Interface, Marker Interface and Serialization in Java

SERIALIZATION is the process of converting an object into streams i.e. a sequence of bytes. It can be used in a class by implementing its interface by writing serializable. Marker Interface is an interface which is empty i.e. has nobody.

  • In Java, programming language interface is nothing but the collection of methods with empty implementations and constants variables ( variables with static and final declarations ). All the methods in an interface are “public and abstract” by default. Since interfaces are abstract in nature so they can not be directly instantiated
  • To define the methods of an interface the keyword “implements” is used. Interfaces are similar to abstract classes but the major difference between these two is that interface have all the methods abstract while in case of abstract classes must have at least one abstract method. Interface combines the two functionality (template and multiple inheritance) of C++ language into one (in itself)
  • In Java language programming, interfaces with no methods are known as marker interfaces. Marker interfaces are Serializable, Clonable, SingleThreadModel, Event listener. Marker Interfaces are implemented by the classes or their superclasses in order to add some functionality
  • An example of the application of marker interfaces from the Java programming language i.e. Serializableinterface: A class implements this interface to indicate that its non-transient data members can be written to an.ObjectOutputStream The privateObjectOutputStream method containswriteObject() a series of testsinstanceof to determine writeability, one of which looks for the interfaceSerializable. If any of these tests fail, the method throws a.NotSerializableException

The process of constructing or converting an object from object stream is known as deserialization

Example:

Program 1 – To Write: A.java

import java.io.*;
class N implements Serializable{
int a, b;
N(int a, int b){
this.a=a;
this.b=b;
}
void display(){
System.out.println("a="+a+" b="+b);
}
}
class A{
public static void main(String[] args) throws FileNotFoundException, IOException{
FileOutputStream fos=new FileOutputStream("details.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
N n=new N(10, 15);
oos.writeObject(n);
oos.close();
} 
}
  • Converting the object into a stream is also known as persistence
  • To persist means to retain something i.e. state of an object

Program 2 – To Read (details.txt): B.java

import java.io.*;

class B{
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException{
FileInputStream fis=new FileInputStream("details.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
N tmp=(N)ois.readObject();
tmp.display();
ois.close();
} 
}

  Note: Save the two programs respectively with the suggested names, compile it and see the output.

  • If a class field or transient field is static then it is not converted into a stream. e.g:
class N implements Serializable{

  int a, b;

  transient int result; 

}

// here in this case object cannot be converted into a stream.

  • Advantages of Serializable

    • Used in RMI – Remote Method Invocation
    • A complex data structure can be converted into a stream
    • If a superclass has implemented the Serializable interface then the subclass does not require to do so again
    • Serialization is an expensive process (data structure associated with serialization will come into existence only when two classes has implemented so)