An instance method m_C can override another instance method m_A iff all of the following are true:
-
m_Chas the same name and descriptor asm_A. -
m_Cis not markedACC_PRIVATE. -
One of the following is true:
m_Ais markedACC_PUBLIC.m_Ais markedACC_PROTECTED.m_Ais marked neitherACC_PUBLICnorACC_PROTECTEDnorACC_PRIVATE, and either (a) the declaration ofm_Aappears in the same run-time package as the declaration ofm_C, or (b) ifm_Ais declared in a class A andm_Cis declared in a class C, then there exists a methodm_Bdeclared in a class B such that C is a subclass of B and B is a subclass of A andm_Ccan overridem_Bandm_Bcan overridem_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.mcan overrideA.m.C.mcan overrideB.mandA.m.D.mcan overrideB.mand, transitively,A.m, but it cannot overrideC.m.