• 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

Easy Protected Member Question

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

This question is from Complete Java 2 Certification Study Guide 5th Edition :

True or false: If class Y extends class X, the two classes are in different packages, and class X has a protected method called abby(), then any instance of Y may call the abby() method of any other instance of Y.

A. True
B. False

I chose true but the book states the correct answer as B.

Here's the explanation given by the author :

"An object that inherits a protected method from a superclass in a different package may call that method on itself but not on other instances of the same class."

I think the author is partially correct. The below code shows the opposite and compile and runs fine.



I think the author is partially correct or I'm missing something really important.

Any comments would be highly appreciated.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are right too. From what I know, if class Super and Sub are in different packages, and Sub extends Super, then of course, you CANNOT access Super's protected members from Sub by using Super's references. But, you CAN use that Super's protected member by (1) directly calling that member, or (2) using a sub class reference.

-V
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, you can even invoke private methods on a reference of the own class. So you can invoke protected methods as well.
But the point is, that you can not invoke private or protected methods on a reference of the parent class.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic