• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Protected Visibility - what am I missing?

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


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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Jaspreet Singh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
We can walk to school together. And we can both read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic