Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

“Sybex Practice Tests 11” error - Chapter 3, Page 119, Question #213

 
Ranch Hand
Posts: 127
5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I do not see why the answer E is correct. Please help. Thanks!

The book's answers are A, C, and E.
My answers are only A and C. I don't see why the answer E is correct. There is one private method in Michael's interface that is not accessible in the Twins class. So E cannot be correct.

What statements about the following Twins class are true? (Choose three.)

A. The class fails to compile because of the write() method.
B. The class fails to compile because of the publish() method.
C. The class fails to compile because of the think() method.
D. All of the methods defined in the Alex interface are accessible in the Twins class.
E. All of the methods defined in the Michael interface are accessible in the Twins class.
F. The Twins class cannot be marked abstract.

 
Bartender
Posts: 1737
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree.

This question is testing several different pieces of knowledge.

Regarding A, B, C:
All abstract and default methods in an interface are public by default, and can not be made anything less than public in an implementing class.
This is an annoying, but basic Java gotcha, as all methods in classes default to <package> or default access, so those must be explicitly declared public in Twins.
So A and C are both reasons the class fails to compile.
Static methods are not inherited, they are available to an implementing class just the same way they are available to anyone else who can see the interface, so B is not true.

D and E are tricky as hell.  The non-static private methods defined in Alex and Michael interfaces can be re-declared in the implementing class, and can be private, default, protected or public.
So the names are available for use, but there is no way that the private methods in the interfaces themselves can be called directly from the implementing class.
The class could call a default method of the interface that calls them indirectly,  I just confirmed that by adding calls to the private methods of each interface to their default method write, and that of course works when an instance method of Twins explicitly calls the interfaces default write() methods using and called from Twins.write()

I would not interpret that as either D or E being a correct choice.  They can't be directly accessed from the Twins class.

So there are several very tricky things they could potentially have asked, but I conclude they most likely meant to mark only one of those as private and perhaps meant to mark the other one as default, or the answer E is just wrong.  If you are missing something here, so am I.

Eventually one or more of the authors will come by to tell us what they meant to do in this question.
 
author & internet detective
Posts: 41964
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've added this to the errata. We meant non-private methods. Got distracted by the default method that didn't even notice the private one!
 
reply
    Bookmark Topic Watch Topic
  • New Topic