J.II · Java 25 practice question

Class A declares a package-access instance method mA. Subclass C is declared in a different package and defines an instance method mC with the same signature. Does mC override mA?

Choose one answer
Check answer and explanation

Correct answer: No, because a package-access method can only be overridden by a subclass in the same package.

Explanation

The method mC does not override mA because overriding a package-access method requires mA to be declared in the same package as C.

Common trap

Package-access methods are invisible outside their package, so subclasses in other packages declare an unrelated method rather than overriding. Widening mC to public does not create an override relationship when mA is package-access in another package.

Source: JLS SE 25 §8.4.8.1. Overriding (by Instance Methods)
Checked against the cited text on 2026-11-25.

Report an error in this question