0

I know from school that it's impossible to override a private method. Despite that, it seems that redefining the methods writeObject and readObject in a Serializable class, they act as overriden public/protected methods.

private void writeObject(java.io.ObjectOutputStream out)
         throws IOException {

}
private void readObject(java.io.ObjectInputStream in)
         throws IOException, ClassNotFoundException {

}

I don't know the implementation of serialization in the JVM... but this looks as a dirty trick to make the things working.

Am I right, or am I missing something?

Michele Da Ros
  • 856
  • 7
  • 21
  • 1
    I did not understand your doubt. its true that we cannot override a private method. Serializable is a marker interface which we mostly implement for better passage on the network – Saurabh Jhunjhunwala Apr 07 '15 at 07:42
  • It is a "dirty trick". Serialization has plenty of those, for some necessary purposes (such as creating objects without calling a constructor). – Kayaman Apr 07 '15 at 07:53

0 Answers0