Leszek Pachura
Leszek Pachura

Reputation: 569

Class field is a non-serializable interface, but its implementation is Serializable. Will it get serialized?

If I serialize an instance of Parent with field3 being assigned an instance of MyImplementation, will field4 and field5 be serialized as well?

public class Parent implements Serializable {
    public String field1;
    public String field2;
    public MyInterface field3;
}

public interface MyInterface {
    String getText();
}

public class MyImplementation implements MyInterface, Serializable {
    private static final long serialVersionUID = 12345464324324L;
    public int field4;
    public int field5;

    @Override
    public String getText() {
        return "foo";
    }
}

Upvotes: 1

Views: 77

Answers (1)

Leszek Pachura
Leszek Pachura

Reputation: 569

It turns out they will!

PS. Although you'll get a FindBugs warning

"SE_BAD_FIELD - Non-transient non-serializable instance field in serializable class".

Upvotes: 2

Related Questions