This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes About Serializable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "About Serializable" Watch "About Serializable" New topic
Author

About Serializable

suraj savaratkar
Ranch Hand

Joined: May 17, 2010
Posts: 67
Hi to all,

I have an Question about Serialization.

ObjectOutputStream having an writeObject() method.

When we are override in subclass
why its signature changed

Like

private void writeObject(ObjectOutputStream oos)
{
}


Why it make as private.
But in api is

public final void writeObject(Object obj)

As well as ObjectInputStream has readObject()

Syntax:

private void readObject(ObjectInputStream ois)
{
}


but this method does return void so how we are collect an object.
but see in api its signature is
public final Object readObject(){}

How should be resolve this Question?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

You are confusing the methods of Object*putStream (the public read/write methods) with the methods used in an object to customize serialization (the private read/write methods). You are not overriding the first methods by creating the second.
Have a look at this article and see if it clears things up.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
suraj savaratkar
Ranch Hand

Joined: May 17, 2010
Posts: 67
But you could run this code,

If this run successfully, then change the assess modifier( public void writeObject(ObjectOutputStream oos){} and public void readObject(ObjectInputStream ois)
{ }
) of readObject/writeObject and run again.

When you run changing the assess modifier that Oerriden method does not call.

It calls a predefine method. As well as private void readObject(ObjectInputStream ois) {} method doesnt having any parameter and here we have to pass the Object.

Why is it so?

Follows the Code:




If this code run error free then what is the concept behind this?

Thanks.

JCE: please UseCodeTags
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

suraj savaratkar wrote:
Why is it so?


Because that's what the API says:

Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures:

private void writeObject(java.io.ObjectOutputStream out) throws IOException
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;

The writeObject method is responsible for writing the state of the object for its particular class so that the corresponding readObject method can restore it. The default mechanism for saving the Object's fields can be invoked by calling out.defaultWriteObject. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.

The readObject method is responsible for reading from the stream and restoring the classes fields. It may call in.defaultReadObject to invoke the default mechanism for restoring the object's non-static and non-transient fields. The defaultReadObject method uses information in the stream to assign the fields of the object saved in the stream with the correspondingly named fields in the current object. This handles the case when the class has evolved to add new fields. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is saved by writing the individual fields to the ObjectOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.

java.io.Serializable
vishal amode
Greenhorn

Joined: Sep 09, 2010
Posts: 1
The following syntax is in Interface ObjectOutput.
void writeObject(Object obj)
throws IOException

class ObjectOutputStream implements ObjectOutput
in this class above method is overriden as following systax.

public final void writeObject(Object obj)
throws IOException

then how can we override writeObject in our serializable class as well as any ANY-ACCESS-MODIFIER Object writeReplace() throws ObjectStreamException
and what writeReplace() method.

As well same for readObject method.

How its possible?



Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
vishal amode wrote:then how can we override writeObject in our serializable class as well as any ANY-ACCESS-MODIFIER

As Joe explained, you are not overriding anything. Your class has nothing to do with ObjectOutputStream, and in particular it's not extending it.
suraj savaratkar
Ranch Hand

Joined: May 17, 2010
Posts: 67
Ok if am i not overriding an anything but ObjectOutputStream and ObjectInputStream implements Serailizable. If this not an way to overriding then tell me another way(with an Example).
But you can saw me override writeObject() method belongs to ObjectOutputStream class with an public access modifier.

public class ThisisSerialization implements Serailizable
{
public void writeObject(ObjectOutputStream oos)
{

}
}

and when you invoke oos.writeObject() it must call from overriden method from ThisisSerialization class if not then chage
access modifierto private

private class ThisisSerialization implements Serailizable
{
public void writeObject(ObjectOutputStream oos)
{

}
}

again invoke oos.writeObject() it must call from overriden method from ThisisSerialization if this call then whats meaning of
this code.

If you know hierarchy of Serailizable please tell me OR send me diagramatically image.

Thanks.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Again: The code you posted can't possibly be a way of overriding those methods, because your class is not extending ObjectOutputStream. It seems you're getting confused because both methods have the same name; I agree that it is confusing, but the javadoc section Joe quoted explains what's going on.

Also note that there is no "hierarchy" to Serializable (because it does not extend any other interface), and furthermore that Serializable does not actually define any methods (it's a so-called marker interface).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

suraj savaratkar wrote:but ObjectOutputStream and ObjectInputStream implements Serailizable.

Ehm, NO. ObjectInputStream implements Closeable, DataInput, ObjectInput and ObjectStreamConstants; ObjectOutputStream implements Closeable, DataOutput, Flushable, ObjectOutput and ObjectStreamConstants. Serializable is in neither list.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
suraj savaratkar
Ranch Hand

Joined: May 17, 2010
Posts: 67
OK! writeObject() and readObject() arent overriden fine.

But could you explain me if i wrote definition for writeObject() and readObject() method in my class with an
private void writeObject(ObjectOutpitStream oos) then oos object invoke writeObject() method from my class.

Why? Why not ObjectOutput Class. Even i change part from private then oos object doesnt call from my class?
How? Why?
Explain me!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

suraj savaratkar wrote: Even i change part from private then oos object doesnt call from my class?


As I posted earlier, the API says:
Classes that require special handling during the serialization and deserialization process must implement special methods with these exact signatures


Why do you expect it to work if you don't use the exact signatures specified by the API?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: About Serializable
 
Similar Threads
serializable interface
Please need help in Serialization
interview question of the day (08/04/2008)
Getting an exception(Serialization)
private(?) writeObject/readObject