Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6814861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:44:20+00:00 2026-05-26T20:44:20+00:00

How are the constructors called during serialization and deserialization When there is one class

  • 0

How are the constructors called during serialization and deserialization

  1. When there is one class implementing serializable?
  2. When there is parent/child relationship and only child implements serializable?
  3. When there is parent/child relationship and both parent and child implements serializable?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:44:20+00:00Added an answer on May 26, 2026 at 8:44 pm

    Example:

     public class ParentDeserializationTest {
    
        public static void main(String[] args){
            try {
                System.out.println("Creating...");
                Child c = new Child(1);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                c.field = 10;
                System.out.println("Serializing...");
                oos.writeObject(c);
                oos.flush();
                baos.flush();
                oos.close();
                baos.close();
                ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
                ObjectInputStream ois = new ObjectInputStream(bais);
                System.out.println("Deserializing...");
                Child c1 = (Child)ois.readObject();
                System.out.println("c1.i="+c1.getI());
                System.out.println("c1.field="+c1.getField());
            } catch (IOException ex){
                ex.printStackTrace();
            } catch (ClassNotFoundException ex){
                ex.printStackTrace();
            }
        }
    
        public static class Parent {
            protected int field;
            protected Parent(){
                field = 5;
                System.out.println("Parent::Constructor");
            }
            public int getField() {
                return field;
            }
        }
    
        public static class Child extends Parent implements Serializable{
            protected int i;
            public Child(int i){
                this.i = i;
                System.out.println("Child::Constructor");
            }
            public int getI() {
                return i;
            }
        }
    }
    

    Output:

    Creating...
    Parent::Constructor
    Child::Constructor
    Serializing...
    Deserializing...
    Parent::Constructor
    c1.i=1
    c1.field=5
    

    So if you deserialized your object, its constructors doesn’t called, but default constructor of its parent will be called.
    And don’t forget: all your serializable object should have a standard constructor without parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why is the Child class's converter constructor called in the code below? I mean,
If a public constructor in an abstract class can only be called by their
Is there a way to suppress the treeview_AfterSelect() event so it isn't called during
So, I just stumbled across this one in another question : Parent constructors are
I have a child and a parent class, as such: class B : A{
1) Refcounted pointers need stack variables to have constructors / destructors called at predictable
I have an array of objects in MATLAB and I've called their constructors in
Which gets called first - the base constructor or other stuff here? public class
Let's say I have a PHP class called Color , it's constructor accepts various
on more than one occasion I felt the need to define class methods that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.