Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7122416

super field access and accessibility

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 8
    • 7
    • specification
    • None
    • generic
    • generic
    • Verified

      JLS3 15.11.2 says:

      "Suppose that a field access expression super.name appears within class C, and the immediate superclass of C is class S. Then super.name is treated exactly as if it had been the expression ((S)this).name; thus, it refers to the field named name of the current object, but with the current object viewed as an instance of the superclass. Thus it can access the field named name that is visible in class S, even if that field is hidden by a declaration of a field named name in class C."

      Per private email, the use of ((S)this).name is unsuitable when accessing a protected field in a superclass in a different package. (Because the receiver variable for name must be C or a subclass of C, which S definitely isn't.)

      JLS7 15.11.2 was updated in 2011 to say:

      "If a field access expression super.name appears within class C, and the immediate superclass of C is class S, then super.name is treated exactly as if it had been the expression this.name in the body of class S. Thus it can access the field name that is visible in class S, even if that field is hidden by a declaration of a field name in class C."

      Unfortunately, an intention discussed in private email to add an accessibility check (from C to 'name' in S) was not incorporated. The text in JLS7 is thus far too powerful, implying that super can ignore access control when in fact super must respect access control.

            abuckley Alex Buckley
            abuckley Alex Buckley
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: