Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ObjectOutputStream.writeObject(Object o); but not ObjectOutputStream.writeObject(Serializable o) [duplicate]

Why does ObjectOutputStream.writeObject(Object o) not take a Serializable? Why is it taking an Object?

like image 458
Rajesh Avatar asked Apr 11 '11 13:04

Rajesh


People also ask

What is ObjectOutputStream in Java?

Basically, the ObjectOutputStream encodes Java objects using the class name and object values. And, hence generates corresponding streams. This process is known as serialization. Those converted streams can be stored in files and can be transferred among networks.

What is serialization in ObjectOutputStream?

This process is known as serialization. Those converted streams can be stored in files and can be transferred among networks. Note: The ObjectOutputStream class only writes those objects that implement the Serializable interface. This is because objects need to be serialized while writing to the stream

What is the difference between retrieve and reset in ObjectOutputStream?

Retrieve the object used to buffer persistent fields to be written to the stream. This method will allow trusted subclasses of ObjectOutputStream to substitute one object for another during serialization. Reset will disregard the state of any objects already written to the stream.

How to write an object to the stream in Java?

The method writeObject is used to write an object to the stream. Any object, including Strings and arrays, is written with writeObject. Multiple objects or primitives can be written to the stream. The objects must be read back from the corresponding ObjectInputstream with the same types and in the same order as they were written.


2 Answers

This is because writeObject in ObjectOutputStream overrides the method in the ObjectOutput interface which does not require that the object be Serializable.

The ObjectOutput interface specifies methods that allow objects to be written to a stream or underlying storage, but this may be achieved by a process other than serialization. The ObjectOutputStream implements this functionality, but requires serializable objects. However, it cannot modify the signature of the interface that it implements.

like image 135
AndyT Avatar answered Oct 15 '22 04:10

AndyT


It should be ObjectOutputStream.writeObject(serializable) rather than ObjectOutputStream. writeObject(Object). It is a proper use case where a marker interface like Serializable should have been used but unfortunately not. This would have made it possible the very real benefit of compile-time type checking instead of failing at runtime if the object does not implement Serializable interface.

I would like to take this opportunity to mention what Joshua Bloch has mentioned in his book Effective java:

A marker interface is an interface that contains no method declarations, but merely designates (or “marks”) a class that implements the interface as having some property. For example, consider the Serializable interface. By implementing this interface, a class indicates that its instances can be written to an ObjectOutputStream (or “serialized”).

In the case of the Serializable marker interface, the ObjectOutputStream.write(Object) method will fail if its argument does not implement the interface. Inexplicably, the authors of the ObjectOutputStream API did not take advantage of the Serializable interface in declaring the write method. The method’s argument type should have been Serializable rather than Object. As it stands, an attempt to call ObjectOutputStream.write on an object that doesn’t implement Serializable will fail only at runtime, but it didn’t have to be that way.

like image 34
Trying Avatar answered Oct 15 '22 06:10

Trying