Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Errata in Monkhouse/Camerlengo, Listing 6-3, Externalizable (p. 170)

 
Ralf Wahner
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Colleagues

Situation: Listing 6-3 on page 170 partially shows the sourcecode of a hypothetical DVD class implementing the Externalizable interface:

I believe that there are three errors in the above code. Since interface methods are by default public and according to the rules for overriding a method, the access level can't be more restrictive than the overridden method's, the private modifiers should be replaced by public. The API documentation for Externalizable states, that readExternal() can throw two exceptions, namely IOException and ClassNotFoundException (the latter is missing in the book). I suggest, that Listing 6-3 should read:

Confusingly, page 169 (in the middle) says: "Both methods [writeExternal() and readExternal()] can be private since the serialization mechanism can circumvent the normally applicable accessibility rules for classes ...." Am I stupid?

Best regards,

Ralf Wahner
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ralf,

I checked the Errata page of Andrew's book, but no mentioning of these issues. If I google a bit around all the examples I find about implementations/use of interface Externalizable, they all have public readExternal and writeExternal methods and the read-method has also a ClassNotFoundException in the throws clause.

And you are correct: to have a valid method override you can not restrict the visibility. But mentioning the ClassNotFoundException in the method signature of the implementation is not required for a valid override.

But I'm sure Andrew will give his view about these issues in the next days, because he is a regular visitor of this forum.

Kind regards,
Roel

 
Yucca Nel
Ranch Hand
Posts: 147
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralf Wahner wrote:Dear Colleagues

Situation: Listing 6-3 on page 170 partially shows the sourcecode of a hypothetical DVD class implementing the Externalizable interface:

I believe that there are three errors in the above code. Since interface methods are by default public and according to the rules for overriding a method, the access level can't be more restrictive than the overridden method's, the private modifiers should be replaced by public. The API documentation for Externalizable states, that readExternal() can throw two exceptions, namely IOException and ClassNotFoundException (the latter is missing in the book). I suggest, that Listing 6-3 should read:

Confusingly, page 169 (in the middle) says: "Both methods [writeExternal() and readExternal()] can be private since the serialization mechanism can circumvent the normally applicable accessibility rules for classes ...." Am I stupid?

Best regards,

Ralf Wahner


Be glad. It proves you know the basics. No need to question what you know.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. You are correct, and I will have to add an errata entry for it.

I would guess that what Terry was considering was when the class is implementing the Serializable interface - in that case you can have your own private writeObject and readObject methods that will be called during the serialization process.
 
Ralf Wahner
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Colleagues

Thank you for confirming my suspicion. ;-) At first sight, I thought that the private read|writeExternal() methods are correct and that calling these methods occurs by means of the same mysterious mechanism that calls read|writeObject(). Later I noticed, that Externalizable is not a marker interface, and therefore the private modifier cannot come into consideration. I'm sorry for misleading you concerning the ClassNotFoundException with readExternal(); of course "the overriding method can throw narrower or fewer exceptions. Just because an overridden method "takes risks" doesn't mean that the overriding subclass' exception takes the same risks" (Sierra, K. and Bates B., SCJP Study Guide SE5, p. 102).

Good luck and best wishes,

Ralf
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic