aspose file tools*
The moose likes Beginning Java and the fly likes Protected Visibility - what am I missing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Protected Visibility - what am I missing?" Watch "Protected Visibility - what am I missing?" New topic
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected Visibility - what am I missing?