subclass « serialize « Java I/O Q&A

1. Gson doesn't serialize fields defined in subclasses

For some unknown reason if I have:

class A{
int stars;
public int getStars(){
return stars;

public void setStarts(int stars){
this.stars = stars;

class B extends A{
 int sunshines;

[getter and setter for sunshines]

class C{
 List<A> classes;
[get and set ...

2. will subclass gets serialized?

Prashanth, Man, I thought I was going to get off easy on this one. No, the non-serializable superclass of a serializable class is not serialized. Notice I say "the", not "a", because at some point in every class's parentage there is a non-serializable class - Object is not serializable. There are a couple of points to address here: 1) Suppose C ...

4. Best Practices for Serializing Subclass whose Superclass is NOT Serializable

import*; class Player{ Player(){System.out.print("p");} } class CardPlayer extends Player implements Serializable{ CardPlayer(){System.out.print("c");} public static void main(String[] args){ CardPlayer c1 = new CardPlayer(); try{ FileOutputStream fos = new FileOutputStream("play.txt"); ObjectOutputStream os = new ObjectOutputStream(fos); os.writeObject(c1); os.close(); FileInputStream fis = new FileInputStream("play.txt"); ObjectInputStream is = new ObjectInputStream(fis); CardPlayer c2 = (CardPlayer) is.readObject(); is.close(); }catch(Exception x){} } }

5. Regarding Serializable and subclasses.

I have a an abstract parent class called 'Information' which implements Serializable and which has an abstract method called init(). There are 2 subclasses that extend the parent class Information called SInformation_1 and SInformation_2 and implement the method init(). Now as the parent class is Serialized,will the concrete subclasses also be Serializable?

6. Serialize all subclasses automatically??