Thanks for such an elaborative example
This forum is really cool
I now understand that its the order in which the data is written to the stream which is important. The example shows that Dog data is written first to the OutPutStream and then cats data is written to the Outputstream and while reading from the Inputstream the order is maintained.
However if I change the order of reading like the below italic :
Cat c = test.readCat(is);
System.out.println(c.getCode());
System.out.println(c.getName());
Dog d = test.readDog(is);
System.out.println(d.getCode());
System.out.println(d.getName()); then the Dog object is found which cannot be casted to a cat reference and I get ClassCastException which emphasizes the importance of order while writing and reading.
Many Thanks once again!!!