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

  • SEARCH
  • Home
  • 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 7000717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:42:29+00:00 2026-05-27T20:42:29+00:00

Why is constructor of the Food and Fruit class called when I deserialize where

  • 0

Why is constructor of the Food and Fruit class called when I deserialize where as the constructor of SkinFruit and Banana2 is not called in the below example?

Suppose I have following class hierarchy

class Food{
  public Food() { System.out.println("1"); } 
    }

class Fruit extends Food {
    public Fruit() { System.out.print("2"); }
}

class SkinFruit extends Fruit implements Serializable{
    SkinFruit() { System.out.print("3"); }
}
public class Banana2 extends SkinFruit { 
  private static final long serialVersionUID = 1L;
  int size = 42;

     public static void main(String [] args) {
       Banana2 b = new Banana2();
       FileOutputStream fost;
        try {
            fost = new FileOutputStream(new File("d:/testbanana.ser"));
            ObjectOutputStream oostr= new ObjectOutputStream(fost);
            oostr.writeObject(b);
            oostr.flush();          
            oostr.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }

        try {
            FileInputStream fin= new FileInputStream(new File("d:/testbanana.ser"));
            ObjectInputStream objin= new ObjectInputStream(fin);
             b=(Banana2)objin.readObject();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


       System.out.println(" restored "+ b.size + "" );
     }
}
  • 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-27T20:42:29+00:00Added an answer on May 27, 2026 at 8:42 pm

    Neither is called, because your class is not Serializable.

    You cannot call the B() without calling A() because B calls A.

    Consider the following program

    public static void main(String... args) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(new B());
        oos.writeObject(new D());
        oos.close();
        System.out.println("-------------");
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
        System.out.println("deserialize B");
        ois.readObject();
        System.out.println("deserialize D");
        ois.readObject();
    }
    
    static class A {
        public A() {
            System.out.println("Inside class A");
        }
    }
    
    static class B extends A  implements Serializable {
        public B() {
            System.out.println("Inside class B");
        }
    }
    
    static class C implements Serializable  {
        public C() {
            System.out.println("Inside class C");
        }
    }
    
    static class D extends C {
        public D() {
            System.out.println("Inside class D");
        }
    }
    

    prints

    Inside class A
    Inside class B
    Inside class C
    Inside class D
    -------------
    deserialize B
    Inside class A
    deserialize D
    

    A is the non Serializable parent of B. It is not Serialized and so the constructor has to be called to set any fields which need to be set. However, B, C and D are all Serializable and their fields have been serializable and will be reset. There is no need to call the constructor.

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

Sidebar

Related Questions

Suppose the following constructor: class Needed { public: Needed () {} Needed (const char
Constructor and method not working as expected in Java program I have the following
class A constructor: -> method: -> In the above example, method is not bound
I have a constructor for my Arduino-code which is something like the following: class
Which constructor will called first in the below example? When i put break point
I have a PHP class called food. The internal data structure of the class
I have a constructor (for an auto generated class) that has 255 paremeters. Using
If a public constructor in an abstract class can only be called by their
Consider the following constructor for the class Foo (which for the sake of clarity
Constructor Delay.vkMessages.vkMessages(string, System.DateTime, string, bool, string) cannot call itself.I have another class, copy of

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.