| Author |
Protected Visibility - what am I missing?
|
Jaspreet Singh
Greenhorn
Joined: Jul 07, 2005
Posts: 8
|
|
Why is CODE] sub.protectedmethod()[ [/CODE]giving a visibiliy error. Why should it matter to visibility where from (Test) I am invoking the object's method, all that should matter is what is available to the object that I am invoking the method on. And of course, the protectedmethod() is available to MySubClass via inheritence from MyObject.
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
Originally posted by Jaspreet Singh: [CODE] Why should it matter to visibility where from (Test) I am invoking the object's method, all that should matter is what is available to the object that I am invoking the method on. And of course, the protectedmethod() is available to MySubClass via inheritence from MyObject.
True, protectedmethod() is available to MySubClass, but it is NOT available to Test because it's protected. The visibility modifiers (public, protected, and private) affect what other classes are allowed to call a method. Since protectedmethod() is protected, it can only be called by subclasses (and technically by other non-subclasses in the same package as well). Since Test is not a subclass and in a different package, it cannot call protectedmethod(). In order to allow Test to call the method, you need to redeclare it as public in MySubclass. Layne
|
Java API Documentation
The Java Tutorial
|
 |
Jaspreet Singh
Greenhorn
Joined: Jul 07, 2005
Posts: 8
|
|
The visibility modifiers (public, protected, and private) affect what other classes are allowed to call a method.
Well, if thats true, then making Test a subclass of MyObject should allow sub.protectedmethod(), isnt it? It still gives an error.
|
 |
 |
|
|
subject: Protected Visibility - what am I missing?
|
|
|