65 lines
1.2 KiB
Plaintext
65 lines
1.2 KiB
Plaintext
When an inner class extends another class, and both its outer class and its parent class have a method with the same name, calls to that method can be confusing. The compiler will resolve the call to the superclass method, but maintainers may be confused, so the superclass method should be called explicitly, using ``++super.++``.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
[source,java]
|
|
----
|
|
public class Parent {
|
|
public void foo() { ... }
|
|
}
|
|
|
|
public class Outer {
|
|
|
|
public void foo() { ... }
|
|
|
|
public class Inner extends Parent {
|
|
|
|
public void doTheThing() {
|
|
foo(); // Noncompliant; was Outer.this.foo() intended instead?
|
|
// ...
|
|
}
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
[source,java]
|
|
----
|
|
public class Parent {
|
|
public void foo() { ... }
|
|
}
|
|
|
|
public class Outer {
|
|
|
|
public void foo() { ... }
|
|
|
|
public class Inner extends Parent {
|
|
|
|
public void doTheThing() {
|
|
super.foo();
|
|
// ...
|
|
}
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|