Sandeep Bhardwaj
Sandeep Bhardwaj

Reputation: 1360

Why only one parent object is created in case of Serialization/Deserialization

Why only one parent object is created in case of Serialization/Deserialization

//superclass A 
//A class doesn't implement Serializable
//interface.
class A 
{
    int i;

    // parameterized constructor
    public A(int i)
    {
        this.i = i;
    }

    // default constructor
    public A()
    {
        i = 50;
        System.out.println("A's class constructor called");
    }
}

// subclass B implementing Serializable interface
class B extends A implements Serializable
{
    int j;

    public B(int i, int j)
    {
        super(i);
        System.out.println("B.B()");
        this.j = j;
    }
}

// Driver class
public class SerializationWithInheritanceExample
{
    public static void main(String[] args) throws Exception
    {
        B b1 = new B(10, 20);

        System.out.println("i = " + b1.i);
        System.out.println("j = " + b1.j);

        // Serializing B's(subclass) object
        try (FileOutputStream fos = new FileOutputStream("abc.ser");
                ObjectOutputStream oos = new ObjectOutputStream(fos))
        {
            // Method for serialization of B's class object
            oos.writeObject(b1);
        }

        System.out.println("Object has been serialized\n");

        // Reading the object from a file
        readObject();
        readObject();
        readObject();

    }

    static void readObject()
    {
        // Reading the object from a file
        try (FileInputStream fis = new FileInputStream("abc.ser"); ObjectInputStream ois = new ObjectInputStream(fis))
        {
            // Method for de-serialization of B's class object
            B b2 = (B) ois.readObject();

            System.out.println("HasCode of A:"+ b2.getClass().getSuperclass().hashCode() +" | HasCode of B:"+b2.hashCode());

            System.out.println("i = " + b2.i);
            System.out.println("j = " + b2.j);
        } catch (IOException | ClassNotFoundException e)
        {
            e.printStackTrace();
        }
    }
}

Output

B.B()
i = 10
j = 20
Object has been serialized

A's class constructor called
HasCode of A:1311053135 | HasCode of B:1705736037
i = 50
j = 20
A's class constructor called
HasCode of A:1311053135 | HasCode of B:455659002
i = 50
j = 20
A's class constructor called
HasCode of A:1311053135 | HasCode of B:250421012
i = 50
j = 20

While de-serialization of B's object multiple time only one object of class A parent is created. Why only one object is created ?

Upvotes: 3

Views: 104

Answers (3)

Vasily Vlasov
Vasily Vlasov

Reputation: 3346

In the case of A you get hashcode of a class A. In the case of B you get hashcode of instance B. And your 3 instances of B are sharing 1 superclass A (along with class B). So, for example, if you call b2.getClass().hashcode() 3 times the output will be equal for all these method invocation too, because you have 1 class B.

Upvotes: 0

Mark Rotteveel
Mark Rotteveel

Reputation: 109090

You are not calling the hashCode() method of an instance of A, but of the class A, there is - in most situations - only one instance of the class object.

Lets break it down:

b2               // instance of B
.getClass()      // Class<B>
.getSuperclass() // Class<A>
.hashCode()      // hash code of Class<A>

It is not even possible to get a separate hash code for the 'parts of A' of an instance of B: there is only one object, and it has only one hash code.

When you create a B, only one object is created, not two like you seem to think. This object contains all of B, which includes the parts of A.

Upvotes: 1

Vladimir Parfenov
Vladimir Parfenov

Reputation: 665

When you call b2.getClass().getSuperclass() you have an instance of type Class<A>. This is only object for class A, which has information about declaration of class A. When you call b2.hashCode() you have hash code of instance, which referred to b2.

Upvotes: 1

Related Questions