I have one question
You must be signed in to leave a comment
15 February, 21:23
If you run the original code (after making Solution Serializable) you will see that nameB is being serialized while nameA is not. Now take this code as an exampleI removed the A class completely while putting the nameA field in the B class. If you run it you will see that both the fields have serialized properly. This is because serializable classes will have all their fields serialized and deserialized wiht just the default read/write object methods.
15 February, 21:34
The problem with the original code (after adding Serializable to Solution and a no args constructor to A) is that class A is not serializable so the defaultWriteObject() method is not serializing the nameA field that is part of the A class. You could fix this by implementing the Serializable interface on the A class, however the task conditions specifically state that you should not do this. This means that you are specifically going to need to write, and subsequently read, nameA in the proper methods. It looks like you have already figured this all out, hopefully I was able to explain it a little to make more sense. You do have one problem in your code in that the B constructor should still be the same:When creating new B objects the constructor with one argument in A should be called but that change in your constructor would call the no args constructor and be a potential bug. I suggest adding that line back.
16 February, 08:26
Step after step it's more clearer. Thanks guys!
15 February, 21:16
to serialize nameB you either can go for read- writeObject or you use the defaultRead- WriteObject methods. Which one you chose does not matter here. Just use the same for reading and writing