• Post Reply Bookmark Topic Watch Topic
  • New Topic

!! protected with static method access modifiers in child class of other package OCA Question  RSS feed

 
gaurav pathak zack
Greenhorn
Posts: 1
Eclipse IDE Hibernate Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the below code method 'm1' in classA is declared as static with protected keyword, Why compiler is allowing this part to access with parent reference in child class of packageb. As what I understood with protected definition as "A protected member is accessible within all classes in the same package and within sub-classes in other packages using child reference"

    

If I will remove the static keyword from method m1 the same code will throw me the error. Why so? Error without static keyword (Adding error on request):

packageb\ClassB.java:6: error: m1() has protected access in ClassA a.m1(); ^
packageb\ClassB.java:9: error: m1() has protected access in ClassA a1.m1(); ^ 2 errors


I have asked the same question in stackoverflow and I think the question has been marked duplicate by mistake and I didn't got got the answer what I was expecting.

Thanks in advance.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
547
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gaurav pathak zack wrote:If I will remove the static keyword from method m1 the same code will throw me the error. Why so? Error without static keyword (Adding error on request):

packageb\ClassB.java:6: error: m1() has protected access in ClassA a.m1(); ^
packageb\ClassB.java:9: error: m1() has protected access in ClassA a1.m1(); ^ 2 errors

I'm not actually sure. The good news is that this isn't on the OCA exam. Reasoning through, I understand why it doesn't work without the static. (The protected lets class A call it via super() but not as an object). But that doesn't explain why it works when the static modifier is there.

I've added some flags to this post to get it some more attention. Great question.

gaurav pathak zack wrote:I have asked the same question in stackoverflow and I think the question has been marked duplicate by mistake and I didn't got got the answer what I was expecting.

No problem. Cross posting is fine as long as you disclose that you did so. (direct link to the SO post)
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have time to look through the JLS right now but I suspect it has something to do with "being responsible for the implementation of an object"
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLS 8 wrote:6.6.2.1. Access to a protected Member
Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C.

In addition, if Id denotes an instance field or instance method, then:

  • If the access is by a qualified name Q.Id or a method reference expression Q :: Id (§15.13), where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.
  • If the access is by a field access expression E.Id, or a method invocation expression E.Id(...), or a method reference expression E :: Id, where E is a Primary expression (§15.8), then the access is permitted if and only if the type of E is S or a subclass of S.
  • If the access is by a method reference expression T :: Id, where T is a ReferenceType, then the access is permitted if and only if the type T is S or a subclass of S.

  • https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6.2

    So, the additional rules apply only to instance members.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!