R
Roedy Green
I just noticed that it is writeObject ( Object o )
rather than writeObject ( Serializable o )
how come?
rather than writeObject ( Serializable o )
how come?
rather than writeObject ( Serializable o )
I wondered the same thing, same for readObject.
Seems to me this is a design flaw.
My best explanation is that you are going to do a cast check anyway on
reconstitution, and so the cast check to Serializable it would have to
do internally would be wasted.
Roedy Green said:I just noticed that it is writeObject ( Object o )
rather than writeObject ( Serializable o )
how come?
It's obvious.
It's obvious.
Roedy said:I just noticed that it is writeObject ( Object o )
rather than writeObject ( Serializable o )
how come?
IMO: I'd say that this is because writeObject writes Objects. In it's
default implementation it writes only objects that have implemented
Serializable. But one may want to provide it's own method to write
Objects, not relying on Serializable.
Bye
Michael
Roedy said:I just noticed that it is writeObject ( Object o )
rather than writeObject ( Serializable o )
how come?
lyallex said:I thought this might be the answer until I realised that writeObject
is final and therefore can't be overridden ... there is a method
called writeObjectOverride(Object obj) that the API docs say should be
used to 'override' the default writeObject method (?) but this just
seems to reinforce the original notion that writeObject should take an
argument of type Serializable.
Carl said:Because it can also write Externalizable objects. And because the
behavior of Externalizable and Serializable are different enough,
neither one is a superinterface of the other.
Michael said:Externalizable extends Serializable.
Michael said:Serializable doesn't declare any methods. If we would rely only on the
Serializable interface we wouldn't be able to use any method. E.g. there
would be no way to call getClass (because Serializable doesn't define it).
IMO: I'd say that this is because writeObject writes Objects. In it's
default implementation it writes only objects that have implemented
Serializable. But one may want to provide it's own method to write
Objects, not relying on Serializable.
Ok, then let's assume that overriding isn't the argument. Even so, I
don't think that writeObject should take a Serializable. I think that
this has something to do with design.
If writeObject would take an argument of type Serializable, this would
only work because AFAIK all objects in (current) Java are instances of
Object.
The most experienced java developer/architect I know is my dog.lyallex said:Ah, great, can you tell us then.
I have just asked the most experienced java developer/architect I know
and he didn't know the answer ...
Lee said:Actually, a reference whose type is an interface can utilize the methods declared for
Object. For example,
Serializable thing = new Integer(1);
Class thingClass = thing.getClass();
... will work just fine.
Roedy said:that begins to make sense.
However I found I could do this:
Serializable s = ...
fos.writeObject( s );
So serialisables are considered also objects.
Michael said:Due to Javas architecture, anything (AFAIK) is an instance of Object.
Therefore you can cast anything to Object.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.