Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Visibility limits of protected access Specifier  RSS feed

 
Pavan Chillara
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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 ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • 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" ?
 
Deepthi Bollu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!