• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Greenhorn
Posts: 5
Hibernate Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 42134
937
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Bartender
Posts: 15741
368
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic