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 ..
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 ]
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" ?
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.