title: How is it possible to override final writeObject() method in Custom Serialization (Java)
body: <p>I'm learning about custom serialization and I don't understand how is it possible to overide the 2 methods writeObject() and readObject() because I know these 2 methods are final, and I know that final methods cannot be overriden.</p>
  
  <p>writeObject() method from ObjectOutputStream:</p>
  
  <pre><code>public final void writeObject(Object obj) throws IOException
  </code></pre>
  
  <p>And writeObject() needs to be overriden like here:</p>
  
  <pre><code>private void writeObject(ObjectOutputStream output) throws IOException
  </code></pre>
  
  <p>I understand that the new writeObject() method is private, but it is called using reflection by Java serialization mechanism. But I don't understand how is it possible to override a final method. </p>
  
  <p>Account Class:</p>
  
  <pre><code>import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.Serializable;
  
  public class Account implements Serializable {
  
      private static final long serialVersionUID = 154754873L;
      String userName = "durga";
      transient String psw = "anushka";
  
      private void writeObject(ObjectOutputStream output) throws IOException {
  
          output.defaultWriteObject();
          output.writeObject(psw);
      }
  
      private void readObject(ObjectInputStream input) throws IOException, ClassNotFoundException {
  
          input.defaultReadObject();
          psw = (String) input.readObject();
      }
  }
  </code></pre>
  
  <p>SerializationDemo Class:</p>
  
  <pre><code>import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  
  public class SerializationDemo {
  
      public void serialize(Account a1, String fileName) {
  
          try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName))) {
  
              oos.writeObject(a1); 
  
          } catch (FileNotFoundException ex) {
  
              System.out.printf("ERROR: %s", ex);
  
          } catch (IOException ex) {
  
              System.out.printf("ERROR: %s", ex);
          }
      }
  
      public Account deserialize(String fileName) {
  
          Account a2 = null;
  
          try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("account.ser"))) {
  
              a2 = (Account) ois.readObject();
          } catch (FileNotFoundException ex) {
  
              System.out.printf("ERROR: %s", ex);
  
          } catch (IOException | ClassNotFoundException ex) {
  
              System.out.printf("ERROR: %s", ex); 
          }
  
          return a2;
      }
  }
  </code></pre>
  
  <p>SerializationApp Class:</p>
  
  <pre><code>public class SerializationApp {
  
      public static void main(String args[]) {
  
          Account a1 = new Account();
  
          System.out.println(a1.userName + " " + a1.psw);
  
          SerializationDemo demo = new SerializationDemo();
  
          demo.serialize(a1, "account.ser"); 
  
          Account a2 = demo.deserialize("account.ser");
  
          System.out.println(a2.userName + " " + a2.psw);
      }
  }
  </code></pre>