Page 1 of 1
About Question enthuware.ocpjp.v8.2.1752 :
Posted: Thu Mar 31, 2016 9:41 am
by Robbie
Answer A reads: To customize the behavior of class serialization, the readObject and writeObject methods should be overridden.
Since the readObject and writeObject methods are not in any interface technically you are not overriding any methods?
Re: About Question enthuware.ocpjp.v8.2.1752 :
Posted: Thu Mar 31, 2016 11:17 am
by admin
Not sure what you mean. If these were in an interface, you would "implement" them, not "override" them. It is precisely because these methods belong to a class (ObjectInputStream), you would override them.
Here is what the
JavaDoc for this method says:
Default deserializing for a class can be overriden using the writeObject and readObject methods.
.
Re: About Question enthuware.ocpjp.v8.2.1752 :
Posted: Thu Mar 31, 2016 11:44 am
by Robbie
If these were in an interface, you would "implement" them, not "override" them.
Well when you implement an interface method in a concrete class you can still use the @Override annotation so technically it still is an override or at least Java seems to see it that way.
But in the case of ObjectInputStream yes it would indeed be an override.
However there is another case where in the actual object you are serializing you can add two methods:
Code: Select all
private void writeObject(ObjectOutputStream os) { }
private void readObject(ObjectInputStream is) { }
In question enthuware.ocpjp.v8.2.1389 in the explanation of the last answer there is an example of this approach.
Those methods will get called automatically if they exist but they are not in any interface so it is not an override.
To me it is not clear if answer A means the methods in ObjectInputStream which would be an override or the methods in the actual object you are serializing which would not be an override and you can use both to custimize the serialization behavior.
Re: About Question enthuware.ocpjp.v8.2.1752 :
Posted: Thu Mar 31, 2016 12:53 pm
by admin
OK, I see what you mean. Will make the option clear about what it means. In general though, if you see a method name, and if it is same as the one that belongs to a known standard class, you should assume it to be of that class instead of assuming it being a user defined method.
-Paul.
Re: About Question enthuware.ocpjp.v8.2.1752 :
Posted: Thu May 26, 2016 2:02 pm
by RAZER-KIEV
In other question (enthuware.ocpjp.v8.2.1389) it was used another explanation "Make bonds array transient in Portfolio and IMPLEMENT readObject and writeObject methods to read and write the state of Bond objects explicitly." it makes me confused too.
Re: About Question enthuware.ocpjp.v8.2.1752 :
Posted: Thu May 26, 2016 11:13 pm
by admin
Fixed.
thank you for your feedback!