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 6968289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:25:58+00:00 2026-05-27T16:25:58+00:00

Say you have these two classes, Foo and Bar where Bar extends Foo and

  • 0

Say you have these two classes, Foo and Bar where Bar extends Foo and implements Serializable

class Foo {

public String name;

public Foo() {
    this.name = "Default";
}

public Foo(String name) {
    this.name = name;
}
}

class Bar extends Foo implements java.io.Serializable {

public int id;

public Bar(String name, int id) {
    super(name);
    this.id = id;
}
}

Notice that Foo doesn’t implement Serializable. So what happens when bar is serialized?

    public static void main(String[] args) throws Exception {

    FileOutputStream fStream=new FileOutputStream("objects.dat");
    ObjectOutputStream oStream=new ObjectOutputStream(fStream);
    Bar bar=new Bar("myName",21);
    oStream.writeObject(bar);

    FileInputStream ifstream = new FileInputStream("objects.dat");
    ObjectInputStream istream = new ObjectInputStream(ifstream);
    Bar bar1 = (Bar) istream.readObject();
    System.out.println(bar1.name + "   " + bar1.id);

} 

it prints “Default 21”.
The question is, why the default constructor get called when the class is not serialized?

  • 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-27T16:25:59+00:00Added an answer on May 27, 2026 at 4:25 pm

    Serializable is just a “marker interface” for a given class.

    But that class must adhere to certain rules:

    http://docs.oracle.com/javase/1.5.0/docs/api/java/io/Serializable.html

    To allow subtypes of non-serializable classes to be serialized, the
    subtype may assume responsibility for saving and restoring the state
    of the supertype’s public, protected, and (if accessible) package
    fields. The subtype may assume this responsibility only if the class
    it extends has an accessible no-arg constructor to initialize the
    class’s state. It is an error to declare a class Serializable if this
    is not the case.

    to answer @Sleiman Jneidi question asked in comment,
    in oracle documentation mentioned above, its clearly mentioned

    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.

    Thus, default no-arg constructor of class Foo called of, resulted in initialization.

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

Sidebar

Related Questions

Let's say we have these two classes: public class Base { public static int
Say you have these two classes. public class Author { public int ID {get;
Say I have two classes like this: class A{ private static Random random =
Let's say I have these classes: public class BaseMapClass<T1, T2> { protected T1 Thing1;
I have two classes say Foo and Bar mapped as @OneToOne (bidirectional) using Hibernate
Say I have: class A(val foo: String) class B(foo: String) extends A(foo) class C(val
Say I have two classes One, the Foo class, interfaces with the user, and
Let's say I have these two arrays: string[] arr1 = new string[2]{Hello, Stack} string[]
Say we have a class inheriting from two base classes (multiple inheritance). Base class
Let's say we have two classes, Foo and Foo Sub, each in a different

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.