I have a child class with two instance variables color and position. The constructors takes in both of these. I first call the super(color) constructor which contains setColor(color)->this.color = color. According to debugging tool, this is setting this.color for the super class rather than the child class which invoked the method. Is this not the proper method of using a super class constructor to set instanced variables?
Rook.java:
public Rook(String color, Position pos) {
super(color);
// at this point this.color == null (for Rook)
initPosition(pos);
}
GenericPiece.java:
public GenericPiece(String color) {
setColor(color);
// at this point this.color == "white" (for GenericPiece)
}
public void setColor(String color) throws InvalidPieceColorError {
if (Color.checkValid(color)) {
this.color = color;
} else {
throw new InvalidPieceColorError(color);
}
}
Rookalso define a field namedcolor? I would guess you have a shadowing field namedcolor. Unfortunately, your screenshot does not show enough of the code to be sure. Nor did you post a minimal, reproducible example. So I can only guess.