5.4.5 Method Overriding

An instance method mC can override another instance method mA iff all of the following are true:

  • mC has the same name and descriptor as mA.

  • mC is not marked ACC_PRIVATE.

  • One of the following is true:

    • mA is marked ACC_PUBLIC.

    • mA is marked ACC_PROTECTED.

    • mA is marked neither ACC_PUBLIC nor ACC_PROTECTED nor ACC_PRIVATE, and either (a) the declaration of mA appears in the same run-time package as the declaration of mC, or (b) if mA is declared in a class A and mC is declared in a class C, then there exists a method mB declared in a class B such that C is a subclass of B and B is a subclass of A and mC can override mB and mB can override mA.

      Part (b) of the final case allows for "transitive overriding" of methods with default access. For example, given the following class declarations in a package P:
      public class A           {        void m() {} }
      public class B extends A { public void m() {} }
      public class C extends B {        void m() {} }
      and the following class declaration in a different package:
      public class D extends P.C { void m() {} }
      B.m can override A.m.
      C.m can override B.m and A.m.
      D.m can override B.m and, transitively, A.m, but it cannot override C.m.
贡献翻译,请加 QQ: 840750575    点击这里给我发消息