Suppose I have a java class, MyClass
, that is not inherited from any class. MyClass
implements Serializable
.
Can MyClass
be Serializable
with no other conditions?
Does it depend on what objects MyClass
contain, and if these objects are Serializable
themselves?
For example in the class below, if Mappedoodle2
does not implement Serializable
, will I get an exception when Mappedoodle
is serialized?
import java.io.Serializable;
public class Mappedoodle implements Serializable {
private static final long serialVersionUID = -1760231235147491826L;
private String text;
private int value;
private Mappedoodle2 mm = new Mappedoodle2();
public Mappedoodle(String text, int value) {
this.text = text;
this.value = value;
}
public String getText() {
return text;
}
public int getValue() {
return value;
}
@Override
public String toString() {
return text + ", " + value;
}
}
First, all classes technically extend from
java.lang.Object
, so evenMyClass
will inherit from it.MyClass
can implementSerializable
just by implementing the interface, like any other interface. If a class contains properties of a class that implementsSerializable
, the parent class need not implementSerializable
itself.