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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:00:14+00:00 2026-06-13T14:00:14+00:00

I was reading the Wikipedia article on DD and jumped over to the Double

  • 0

I was reading the Wikipedia article on DD and jumped over to the “Double dispatch in Java and an example” link given at the end. The description of the following Serializable example seems rather confusing to me:

A a = new A();
ObjectOutputStream oos = new ObjectOutputStream();
oos.writeObject( a);

Here’s the description:

In order to serialize A, ObjectOutputStream first looks to see if the method writeObject( ObjectOutputStream oos) exists. If it does, then it calls that method with itself as an argument. The writeObject method then dispatches the call back to the ObjectOutputStream (thus making this a double dispatch). In making this very simple call back, ObjectOutputStream has said: “I’m delegating back to you the responsibility of writing your state onto this stream”. In making that simple choice, ObjectOutputStream has decoupled itself from our object A. Object A in turn says ok, here is some state that I want written on the stream. If the value is a primitive, then it can be dealt by the overloading of the write method. If not, the back and forth can continue at the next level down in the objects graph until all of the useful state has been placed on the stream.

I’m guessing that the description might be confusing because what is being described is what is happening behind the scenes and not the code presented because otherwise it doesn’t seem to make much sense. Here are the parts that confuse me:

  • “ObjectOutputStream first looks to see if the method writeObject( ObjectOutputStream oos) exists”. Why would ObjectOutputStream need to check for the existence of this method since it is its own method?
  • “If it does, then it calls that method with itself as an argument”. It looks to me like it’s calling writeObject with an instance of A as an argument. Going back to the previous item, why would it look for the writeObject signature with an ObjectOutputStream arg if it’s being called with an instance of A?
  • “The writeObject method then dispatches the call back to the ObjectOutputStream (thus making this a double dispatch)”. Again, the writeObject method belongs to the ObjectOutputStream class so I fail to see how this gets “dispatched back to the ObjectOutputStream” since that appears to be the original destination.

Am I just missing something fundamental here or is this a poorly written/described example? If it is a bad example, I’d like to change the Wikipedia article to point to a better one so feel free to provide suggestions.

Thanks.

  • 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-06-13T14:00:16+00:00Added an answer on June 13, 2026 at 2:00 pm

    To answer your questions in order:

    1. ObjectOutputStream.writeObject(Object) checks its argument (using reflection) to determine if the object implements writeObject(ObjectOutputStream). That is, it more or less asks the object, “Do you know how to write your structure to an ObjectOutputStream?”

    2. If the answer is “yes”, then the ObjectOutputStream calls the object’s writeObject(ObjectOutputStream) method, passing itself (the ObjectOutputStream, not the object) as the argument. It basically says, “Good. Write your structure to me.”

    3. The implementation of writeObject(ObjectOutputStream) in the object calls on the ObjectOutputStream to write elements of itself. It is not supposed to call oos.writeObject(this) again.

    For example, suppose I had an object like this:

    class A {
        int x;
    }
    

    If I wanted to enable it to write itself to an ObjectOutputStream, I might expand it like this:

    class A implements Serializable {
        int x;
        private void writeObject(ObjectOutputStream stream) throws IOException {
            stream.writeInt(x);
        }
    }
    

    Then this code:

    A a = new A();
    ObjectOutputStream oos = . . .;
    oos.writeObject(a);
    

    would use the writeObject method in class A to write a, rather than using reflection to write all the (non-transient) fields of A. (In this case, however, there would be no difference.)

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

Sidebar

Related Questions

I was reading a Wikipedia article about Java EE application servers here: http://en.wikipedia.org/wiki/Java_Platform,_Enterprise_Edition#Java_EE_5_certified It
I was reading the Wikipedia article on SFINAE and encountered following code sample: struct
I am reading Wikipedia article on difference btween JAVA and C++. One difference is
I'm just reading about malloc() in C. The Wikipedia article provides an example ,
After reading the wikipedia article on WPF architecture, I am a bit confused with
Reading through the Wikipedia article on First-Class functions, there is a nice table of
After reading the Service Oriented Architecture Principles site and the respective Wikipedia article I
I was recently reading an article on the Windows Metafile vulnerability (http://en.wikipedia.org/wiki/Windows_Metafile_vulnerability#Third-party_patch) and I
I was just reading the Wikipedia article on C11 , the new version of
I was reading the Wikipedia article on virtual inheritance. I followed the whole article

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.