10

In Java, we use the writeObject(Object obj) method of ObjectOutputStream to serialize an Object.

But since the method only takes object that implements the java.io.Serializable interface (or it will throw NotSerializableException), why does it still use the Object parameter rather than Serializable, like writeObject(Serializable o)?

Is there any designing consideration?

Trying
  • 14,004
  • 9
  • 70
  • 110
user2268535
  • 109
  • 4
  • The only benefit is that you can sub class ObjectOutputStream with a custom serialization which doesn't have to be Serializable though this breaks the general contract for this class. I suspect that the reason for this is that some serializable types didn't have this interface e.g. arrays like byte[]. These now implement Serializable but I suspect this was not always the case. Arrays still don't follow OOP design in Java particularly well. – Peter Lawrey Nov 18 '13 at 10:39

0 Answers0