51 lines
826 B
Plaintext
51 lines
826 B
Plaintext
![]() |
It is both suspicious and unusual to make direct assignments to super-class fields from a class constructor. Choosing to make such direct assignments, rather than invoking the ``++super++`` constructor that sets them, potentially bypasses important initialization.
|
||
|
|
||
|
|
||
|
== Noncompliant Code Example
|
||
|
|
||
|
----
|
||
|
public class Parent {
|
||
|
|
||
|
protected int start;
|
||
|
public Parent() {
|
||
|
//...
|
||
|
}
|
||
|
|
||
|
public Parent(int start) {
|
||
|
this.start = start;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public class Child extends Parent {
|
||
|
|
||
|
public Child(int start) {
|
||
|
this.start = start; // Noncompliant
|
||
|
}
|
||
|
}
|
||
|
----
|
||
|
|
||
|
|
||
|
== Compliant Solution
|
||
|
|
||
|
----
|
||
|
public class Parent {
|
||
|
|
||
|
protected int start;
|
||
|
public Parent() {
|
||
|
//...
|
||
|
}
|
||
|
|
||
|
public Parent(int start) {
|
||
|
this.start = start;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public class Child extends Parent {
|
||
|
|
||
|
public Child(int start) {
|
||
|
super(start);
|
||
|
}
|
||
|
}
|
||
|
----
|
||
|
|