In java, if we're implementing an interface in a class, we must provide implementation to the methods present in interface., but in case of Serializable interface, we no don't need to provide implementation to the methods presents in the Serializable interface.Why???
-
The Serializable interface does not contain any methods. I'm thinking the TL;DR version is because the need to Serialize data came before Java had Annotations. If you were to do it today, you would probably use an annotation , in the same way JPA uses @Entity, but changing this would break all the code. – Mikkel Løkke May 26 '14 at 14:06
1 Answers
but in case of Serializable interface we no need to provide implementation to the methods present in Serializable interface.Why???
Because it is for marking purposes only and contains no methods to implement.
When a class is "marked" with the Serializable
interface, it's merely means that it should/could be serialize/de-serialize.
From Oracle's docs:
Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.
And from Wikipedia:
An example of the application of marker interfaces from the Java programming language is the Serializable interface. A class implements this interface to indicate that its non-transient data members can be written to an ObjectOutputStream. The ObjectOutputStream private method writeObject() contains a series of instanceof tests to determine writeability, one of which looks for the Serializable interface. If any of these tests fails, the method throws a NotSerializableException.
For example, if the following class would't been marked with the Serializable
interface:
public class Employee implements java.io.Serializable
{
public String name;
...
}
Then the following serialization code would have fail with NotSerializableException
:
Employee e = new Employee();
e.name = "Reyan Ali";
FileOutputStream fileOut = new FileOutputStream("/tmp/employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/employee.ser");
Note to the *.ser
file extension, it is used when writing serialized java objects to files.

- 12,725
- 14
- 66
- 108