45

When I run below program, I am getting exception as

java.io.InvalidClassException: Files.SerializationMain; Files.SerializationMain; no valid constructor
    at java.io.ObjectStreamClass.checkDeserialize(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at Files.SerializationClass.main(SerializationClass.java:71)
Caused by: java.io.InvalidClassException: Files.SerializationMain; no valid constructor
    at java.io.ObjectStreamClass.<init>(Unknown Source)
    at java.io.ObjectStreamClass.lookup(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at Files.SerializationClass.main(SerializationClass.java:61)

I read somewhere that when we serialize any child class then its base class constructor gets fired.

class Parent123
{
    int age;
    String name;

    Parent123(int age,String name) {
        System.out.println("We are in Parent123 Constructor");
        this.age=age;
        this.name=name;
    }  
}

class SerializationMain extends Parent123 implements Serializable {
    int data1;
    String data2;

    SerializationMain(int data1,String data2)
    {
        super(20,"test");
        this.data1=data1;
        this.data2=data2;
    }

    public void setData1(int data1)
    {
        this.data1=data1;
    }
    public void setData2(String data2)
    {
        this.data2=data2;
    }
    public String getData2()
    {
        return data2;
    }
    public int getData1()
    {
        return data1;
    }
}

public class SerializationClass {

    public static void main(String args[])
    {
        System.out.println("Before Creating Object");
        SerializationMain s1=new SerializationMain(10,"Anurag");
        try
        {
            System.out.println("Serializing Object");
            FileOutputStream fis=new FileOutputStream("Test.ser");
            ObjectOutputStream ois=new ObjectOutputStream(fis);
            ois.writeObject(s1);
        } catch(Exception e1) {
            e1.printStackTrace();
        }
        try
        {
            FileInputStream fis=new FileInputStream("Test.ser");
            ObjectInputStream ois=new ObjectInputStream(fis);
            Object o1=ois.readObject();
            SerializationMain s2=(SerializationMain)o1;
        }
        catch(Exception e1)
        {
            e1.printStackTrace();
        }
    }
}//End of SerializationClass
0

4 Answers 4

63

Add implementation of Serializable to parent class.

Sign up to request clarification or add additional context in comments.

4 Comments

This worked for me in Scala. I just had to extend Serializable in the parent class.
Works in Java too. I like this answer much more, because it helps for immutable objects.
For anyone that ran into the headache that I did, in Scala, extend the available Serializable and not java.io.Serializable to avoid this error :)
It seems like it does not work if we do with instead of extends in Scala. Any work around ?
38

Just provide default constructor in both classes (Parent & Child)

During deserialization, the fields of non-serializable classes will be initialized using the public or protected no-arg constructor of the class. A no-arg constructor must be accessible to the subclass that is serializable. The fields of serializable subclasses will be restored from the stream. more

3 Comments

Can I know the reason why I have to provide. What is the concept?
During deserilization fields of non serilizable class would be initilized using default constructor
What about when there are final fields?
15

No, constructors are not at all called if you use Serialization process. Serializable uses reflection to construct object and does not require no arg constructor. But Externalizable requires public no-arg constructor. however, you parent class may require no-arg constructor.refer this article

An object is serializable (itself implements the Serializable interface) even if its superclass is not. However, the first superclass in the hierarchy of the serializable class, that does not implements Serializable interface, MUST have a no-arg constructor. If this is violated, readObject() will produce a java.io.InvalidClassException in runtime.

you wouldn't get this error if you make the parent class serializable or if you provide public no-arg constructor in parent class.

Comments

1

if you're serializing a child class object, the child class must also implement Serializable, even if the parent already does. and vice versa

This is because Java checks the actual runtime type of the object being serialized. If the child class does not implement Serializable, you'll get a java.io.NotSerializableException, even if the parent class does.

proper way to serialize:

declare child and its parent(root) classes all serlizable means id its like parent1->parent2->child implements serlizable then declare parent1 ,parent2 implemets serlizable with default constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.