• 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

Visibility limits of protected access Specifier

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

Here i am facing a problem with protected access specifier.There is one class A which has protected a method named X.B has inherited the Class A and C has inherited the Class B(All these classes are in the same package).When i am trying to call method X in the main() method of C by creating an instance of A,It is showing an error to change the access specifier to protected .But it was already protected.
I know that the protected methods are visible only to the next immediate class.
Can anyone explain the reason ..

Regards,
Pavan
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I know that the protected methods are visible only to the next immediate class


That is wrong. A protected method is visible to any class that extends the class that contains the protected method. It is also visible to any class in the same package as the class containing the protected method.
[ November 06, 2006: Message edited by: Paul Sturrock ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

Pavan Chillara posted November 06, 2006 05:54 AM

(...)All these classes are in the same package
(...)When i am trying to call method X in the main() method of C by creating an instance of A,It is showing an error to change the access specifier to protected .



This cannot be. If they are in the same pack, the method must be visible.

Here's your situation as I understood:


I'm guessing, but did you mean "When i am trying to override method X" ?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pavan -
First things first, a protected method is just not visible only for the immediate subclasses but are visible to all the children, children of children and all.
And speaking of the solution to your problem, you are instantiating Class A in the main of C and then calling the protected method member of A, right. In the first place, is Class A accessible from class C? Check to see the access modifiers on class A. If class A is accessible and subsequently instantiable, then for sure, the protected method present in A could be accessed from class C.
- Deepthi
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic