• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 147
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Our first order of business must be this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic