← 返回首页
Subtle call to inherited method — CodeQL query help documentation CodeQL docs
CodeQL documentation
CodeQL resources

Subtle call to inherited method

ID: java/subtle-inherited-call Kind: problem Security severity: Severity: warning Precision: very-high Tags: - quality - maintainability - readability Query suites: - java-code-quality.qls - java-security-and-quality.qls

Click to see the query in the CodeQL repository

If a call is made to a method from an inner class A, and a method of that name is defined in both a superclass of A and an outer class of A, it is not clear to a programmer which method is intended to be called.

Example

In the following example, it is not clear whether the call to printMessage calls the method that is defined in Outer or Super.

public class Outer { void printMessage() { System.out.println("Outer"); } class Inner extends Super { void ambiguous() { printMessage(); // Ambiguous call } } public static void main(String[] args) { new Outer().new Inner().ambiguous(); } } class Super { void printMessage() { System.out.println("Super"); } }

Inherited methods take precedence over methods in outer classes, so the method in the superclass is called. However, such situations are a potential cause of confusion and defects.

Recommendation

Resolve the ambiguity by explicitly qualifying the method call:

References