All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.java.S2388.html Maven / Gradle / Ivy

There is a newer version: 8.10.0.38194
Show newest version

Why is this an issue?

An inner class that extends another type can call methods from both the outer class and parent type directly, without prepending super. or Outer.this..

When both the outer and parent classes contain a method with the same name, the compiler will resolve an unqualified call to the parent type’s implementation. The maintainer or a future reader may confuse the method call as calling the outer class’s implementation, even though it really calls the super type’s.

To make matters worse, the maintainer sees the outer class’s implementation in the same file as the call in the inner class, while the parent type is often declared in another file. The maintainer may not even be aware of the ambiguity present, as they do not see the parent’s implementation.

How to fix it

Explicitly call the super type’s method by prepending super. to the method call. If the intention was to call the outer class’s implementation, prepend Outer.this. instead.

Code examples

Noncompliant code example

public class Parent {
  public void foo() { ... }
}

public class Outer {
  public void foo() { ... }

  public class Inner extends Parent {
    public void doSomething() {
      foo();  // Noncompliant, it is not explicit if Outer#foo or Parent#foo is the intended implementation to be called.
      // ...
    }
  }
}

Compliant solution

public class Parent {
  public void foo() { ... }
}

public class Outer {
  public void foo() { ... }

  public class Inner extends Parent {
    public void doSomething() {
      super.foo(); // Compliant, it is explicit that Parent#foo is the desired implementation to be called.
      // ...
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy