I am hitting a brickwall with serialization of a subclass of Location in android/java
Location is not serializable. I have a first subclass called FALocation that do
Looks like Location does not have public/protected no-arg constructor. Such a constructor is needed for making it available for serialization in subclass.
http://download.oracle.com/javase/6/docs/api/java/io/Serializable.html says:
To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime.
And same with the words from Serialization specification:
A Serializable class must do the following: ... Have access to the no-arg constructor of its first nonserializable superclass
That would explain why you have problems only in deserialization, because naturally constructor is not called during serialization.
Small example of failing without accessible constructor:
public class A {
public A(String some) {};
private A() {} //as protected or public everything would work
}
public class B extends A implements Serializable {
public B() {
super("");
}
//these doesn't really matter
//private void writeObject(java.io.ObjectOutputStream out) throws IOException { }
//private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { }
}
public class BSerializer {
public static void main(String ... args) throws Exception {
B b = new B();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(b);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
B deserialized = (B) ois.readObject(); //InvalidClassException
}
}