• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Protected access, and code that is responsible for implementation

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

From JLS-6.6.2,


A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.


I'm trying to understand what a responsible code might be: a statement, an expression, or only a class?

To use super to access a hidden protected field, works. Because the expression, i.e. super.Identifier, used to access the protected field is responsible for implementation, as specified above.

From JLS-6.6.2.1,


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.


  • So I can access a protected member of an object in a static method in S, given that the reference expression used to access the member is of type S or a subclass of S.
    But I fail to see how the code used to access a protected non-static member of an object in a static method is responsible for the implementation of the object!

    Basically, how do §6.6.2 and §6.6.2.1 not contradict each other?

    Thanks.
     
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ramsin Khoshaba wrote:
    From JLS-6.6.2,


    A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.


    I'm trying to understand what a responsible code might be: a statement, an expression, or only a class?

    To use super to access a hidden protected field, works. Because the expression, i.e. super.Identifier, used to access the protected field is responsible for implementation, as specified above.



    The "responsible for the implementation of the object" rule wasn't defined for the this or super keyword. Using the this or super keyword, always refers to the same instance, hence, it is always responsible for the implementation.

    The example is this... Let's say you have code in class S, which is trying to access a protected field in superclass C (that is in a different package), and is doing it with a reference variable. Then the type of the reference variable is checked. If the type is of class S, or subclass of S, then code of class S can access protected members of C, as it is an instance that the code is responsible of the implementation.

    Obviously, this rule is used to protect superclass fields from being accessed by siblings. Just because your class is a subclass of a superclass, doesn't mean that you can access the superclass members of different subclass class (ie. a different branch of the hierarchy).

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

    Henry Wong wrote:The example is this... Let's say you have code in class S, which is trying to access a protected field in superclass C (that is in a different package), and is doing it with a reference variable. Then the type of the reference variable is checked. If the type is of class S, or subclass of S, then code of class S can access protected members of C, as it is an instance that the code is responsible of the implementation.



    Given this program,


    line 8 compiles fine (as it should according to §6.6.2.1), but I can't see how the main method is responsible for the implementation of a Bar object.
     
    Henry Wong
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ramsin Khoshaba wrote:
    line 8 compiles fine (as it should according to §6.6.2.1), but I can't see how the main method is responsible for the implementation of a Bar object.



    The "responsible for the implementation" rule is determined by the location of the code. In this case, the main() method is part of the Bar class, so hence, it is responsible for the Bar class. Or think of it this way, you are the developer assigned to implement the Bar class, hence, the code you write (which is the Bar class) should be able to use any reference that IS-A Bar class right?

    Henry
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic