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

Super class private method (or method reference) should be accessible from extending class if former encloses later or both are enclosed

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 7u7, 8
    • tools
    • None
    • b102
    • 8

      Let's consider following code:

      public class MethodTest_Failure1 {
          static class MethodSupplier {
              private void m() {
                  System.out.println("Hi");
              }
          }

          static class MethodInvoker extends MethodSupplier {
              public void invoke() {
                  this.m();
              }
          }

          public static void main(String[] args) {
              new MethodInvoker().invoke();
          }
      }

      Its compilation failed with following error:

      java: cannot find symbol
        symbol: method m()

      However according to following assertion from "6.6.1. Determining Accessibility" the method should be accessible:

      Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

      I would also like to notice that after casting 'this' to MethodSupplier the compilation succeeds:

      public class MethodTest_Ok1 {
          static class MethodSupplier {
              private void m() {
                  System.out.println("Hi");
              }
          }

          static class MethodInvoker extends MethodSupplier {
              public void invoke() {
                  ((MethodSupplier)this).m();
              }
          }

          public static void main(String[] args) {
              new MethodInvoker().invoke();
          }
      }

      The same situation takes place for:
      - method references;
      - when inner class extends its enclosing class;
      - when making invocation without 'this' qualifier.

      The minimized tests are attached for all these cases.

      This was reproduced on Windows7x64 platform for:
      - JDK 8 b102 x64bit;
      - JDK 7u7 x64bit (just invoking methods);

            vromero Vicente Arturo Romero Zaldivar
            grakov Georgiy Rakov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: