9

I was reading about Java serialization and came to know that if Serializable interface is implemented then class is serialized.

But Serializable is a marker interface. Then how does JVM know with which methods serialization or de-serialization should be done?

As per my understanding the methods declared in an interface are called via polymorphism.

I would give an example below to examplain my question.

    public class MySerializable implements Serializable{

    public void serialize(){

      //Some code to serialize to a file output stream.
    }

    public void deSerialize(){

      //Some code to de-serialize to a file input stream.
    }


}

So now how JVM will call the methods serialize / deSerialize?

And if I have to call them manually via code then why compiler should be let known that Serializable interface is implemented?

Sam
  • 2,352
  • 4
  • 32
  • 45
  • 8
    Here is Jon Skeet great answer about java serialization http://stackoverflow.com/a/352133/2069368 and this may be the answer to your question http://javabeanz.wordpress.com/2010/03/20/todays-read-how-java-serialization-works/. – pepuch Jun 14 '13 at 06:19

1 Answers1

-4

Yes, Serializable interface don't have any methods to implement. If you want serialize object, your object must implement Serializable interface. Next, you can do serialization/deserialization procedures with your object, for example using ObjectOutputStream and ObjectInputStream objects.

There are little example.

Class, which need to serialize/deserialize:

import java.io.Serializable;

public class ObjectToSerialize implements Serializable {

    private static final long serialVersionUID = 7526472295622776147L;

    private String firstName;
    private String lastName;

    public ObjectToSerialize(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    @Override
    public String toString() {
        return getFirstName() + " " + getLastName();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ObjectToSerialize that = (ObjectToSerialize) o;
        return firstName.equals(that.firstName) && lastName.equals(that.lastName);
    }
}

Class with serialization/deserialization methods:

import java.io.*;

public class ObjectSerialization {

    public static void saveObject(Serializable object, String path) throws IOException {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path))) {
            oos.writeObject(object);
        }
    }

    public static Object loadObject(String path) throws ClassNotFoundException, IOException {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path))) {
            return ois.readObject();
        }
    }

    public static void main(String[] args) {
        ObjectToSerialize object = new ObjectToSerialize("Eldar", "Agalarov");
        try {
            String path = "C:/object.bin";
            saveObject(object, path);
            System.out.println("Object serialized: " + object);

            ObjectToSerialize deserializedObject = (ObjectToSerialize) loadObject(path);
            System.out.println("Object deserialized: " + deserializedObject);
            System.out.println("They are equals: " + object.equals(deserializedObject));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Also read this guide about serialization

Eldar Agalarov
  • 4,849
  • 4
  • 30
  • 38
  • 6
    I think the above answer is not match with the question. The question is **How does JVM know with which methods serialization or de-serialization should be done?** ...... And then **how JVM will call the methods serialize / deSerialize?** – Necromancer Apr 28 '16 at 02:28