• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Facing some issue with "protected" access modifier

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

Hi Team,
I am trying out some basic code just to get to know more about Access modifiers. As per my understading :
The protected access modifier provides the same access as the default access modifier, with the addition that subclasses can access protected methods and member variables (fields) of the superclass. This is true even if the subclass is not located in the same package as the superclass. Correct me if I am wrong...
Which means I should be able to access the protected member even though it is present in different package ,provided the current class extending the class which have protected method..PFB code...

I am getting the error as --> "The method methodProtected() from the type AccessModifier1 is not visible"
Could you please let me know where i am wrong and Thanks for your time..



 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A protected membr of a class is accessible in code responsible for the implementation of the object. Not simply in the subclass. It is accessible from instance methods in the subclass.
In that static method, you are not implementing an object, simply using the object, so the protected method is “invisible”.
 
Sarah Jay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:A protected membr of a class is accessible in code responsible for the implementation of the object. Not simply in the subclass. It is accessible from instance methods in the subclass.
In that static method, you are not implementing an object, simply using the object, so the protected method is “invisible”.



Sorry Campbell...
I am not able to understand your explaination as a beginner..
Could you please reframe it at basic leave if possible..

 
Saloon Keeper
Posts: 14264
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A protected method maybe only be called on the same type or subtype of the current class.

In AccessModifierTest, you're calling methodProtected() on a variable am1 that is declared as AccessModifier1. Since AccessModifier1 is not a subtype of AccessModifierTest, the method is not visible.

Change the declaration of am1 to AccessModifierTest, and you'll see that it works.
 
Campbell Ritchie
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarah Jay wrote:. . . Could you please reframe it at basic leave if possible..

No. Thee isn't a simpler version.

You can write code for an object and in the code for that object, you can access a protected superclass' member. You are not writing any code for an object, but using an object in a different package. You cannot access a protected member from a static subclass method. The Java® Language Specification (=JLS) has all the details in, but that is no easieer to understand, I am afraid.
 
Sarah Jay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:A protected method maybe only be called on the same type or subtype of the current class.

In AccessModifierTest, you're calling methodProtected() on a variable am1 that is declared as AccessModifier1. Since AccessModifier1 is not a subtype of AccessModifierTest, the method is not visible.

Change the declaration of am1 to AccessModifierTest, and you'll see that it works.



Thanks Stephan....I got it...Just to make sure ..

Now as per my understanding we are not suppose to call a method with super class reference ...Correct me if am wrong


 
Stephan van Hulst
Saloon Keeper
Posts: 14264
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarah Jay wrote:Now as per my understanding we are not suppose to call a method with super class reference


ONLY for protected members.

With all other access modifiers, accessing a member via an object reference with the least specific type possible is good practice.
 
Let nothing stop you! Not even this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic