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

Private class methods interfere with invocations of interface methods


    • Subcomponent:


      This is a very simple scenario. Just a class declaring a private method, and an interface declaring a public method with the same signature. It compiles without error. But the compile-time and runtime semantics don't match.


      public class A {
       private void m() { System.out.println("A.m"); }
       public static void main(String... args) { B b = new C(); b.m(); }

      public interface I {
       public void m();

      public abstract class B extends A implements I {

      public class C extends B {
       public void m() { System.out.println("C.m"); }

      public class Test {
       public static void main(String... args) { B b = new C(); b.m(); }

      java Test
      Exception in thread "main" java.lang.IllegalAccessError: tried to access method A.m()V from class Test

      java A


      Intuitively, a private method in A should be irrelevant, not affecting the behavior of other classes. Per JLS, I.m is a member of B, and that's what the invocations refer to. But per JVMS, a reference to 'B.m()V' resolves to A.m.

      We could address the problem by changing resolution so that it ignores inherited private methods.

      We could also address it by catching the conflict in the JLS and reporting an error (sort of like the "same erased signature" check).


          Issue Links



              • Assignee:
                dlsmith Dan Smith
                dlsmith Dan Smith
              • Votes:
                0 Vote for this issue
                4 Start watching this issue


                • Created: