简体   繁体   中英

How is it possible to override final writeObject() method in Custom Serialization (Java)

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.

writeObject() method from ObjectOutputStream:

public final void writeObject(Object obj) throws IOException

And writeObject() needs to be overriden like here:

private void writeObject(ObjectOutputStream output) throws IOException

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.

Account Class:

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();
    }
}

SerializationDemo Class:

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;
    }
}

SerializationApp Class:

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);
    }
}

It is not possible to override a final method.

But you don't need to.

One way to customize serialization is to provide writeObject and readObject methods (like you did):

private void writeObject(ObjectOutputStream out) throws IOException;
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;

Note that we don't talk about implementing or overriding methods here.

This should be actually enough - I wonder why you need to override writeObject in ObjectOutputStream .

If you want to customize serialization by subclassing the ObjectOutputStream , you still can "override" how objects are written. For this, subclass ObjectOutputStream , in the subclass call super() and override writeObjectOverride . If you call the protected ObjectOutputStream() constructor, the enableOverride flag is set to true and the final method writeObject (which you can't override) delegates to writeObjectOverride (which can be overridden).

You are confused.

  1. ObjectOutputStream.writeObject() is final and you cannot override it.
  2. The private void writeObject(ObjectOutput) method you can use for custom serialization goes into your own Serializable class, not into a class that extends ObjectOutputStream . The question of overriding does not arise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM