JVM Knowledge

An instance method m_C can override another instance method m_A iff all of the following are true:

  • m_C has the same name and descriptor as m_A.

  • m_C is not marked ACC_PRIVATE.

  • One of the following is true:

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

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() {} }

then:

  • 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.