Search code examples
javaserializationrmi

How does serialization work in Java if an object is upcast before serialization?


Say I have an interface and an object:

public interface MyInterface {
    public String getStringOne();
}

public class MyObject implements MyInterface {
    private final String stringOne = "string1";
    private final Image myGiganticImage; // loads from disk in the constructor

    // Getters for both would follow
}

What gets transferred if I upcast MyObject to MyInterface before sending it across the wire?

public class MyService {
    private final MyObject data = new MyObject();

    public MyInterface getData() {
        return data;
    }
}

Specifically, does myGiganticImage get sent across the wire?


Solution

  • Obviously the state of MyObject is serialized. public MyInterface getData() just enforces that the type returned by getData() is-a MyInterface.