1)why we have used writeInt in line three?what it does and what is the use of this in this program?
First, we can only speculate -- as we don't have any context on what is the implementation for.
It looks like programmer don't want to serialize the Collar object -- the comment states that it isn't possible to serialize it.... However, the Dog object needs it. And it looks like it is possible to simply get another Collar upon deserialization -- provided the collar size is correct. So, the programmer sent the dog collar size as part of the serialized stream.
kiruthigha rajan wrote:
2)does the methods writeObject(ObjectOutputStream os) and readObject(ObjectInputStream is) is the altenative of
No. This is not an alternative. The first snippet is a modification on how the object should be serialized (specifically the components related to the Dog class only). This set of snippets is the actual serialization and deserialization of the object. This set of code will call the other set of code when it is time to serialize the Dog class component of the Dog object.