I have this method where I want to be able to determine if two cells are equal, where “equal” means they have the same position. I have written this code where I use both instanceof and casting to make sure that my object is of the type Position and then cast it to the type Position, but it does not seem to work for some reason.
Here is my code:
public class Postion {
private int column;
private int row;
public Position (final int column, final int row) {
this.column = column;
this.row = row;
}
public int getColumn() {
return column;
}
public int getRow() {
return row;
}
public boolean equals(final Object other) {
if (other instanceof Position) {
Position position = (Position) other;
return ((column == other.getColumn()) && (row == other.getRow()));
} else {
return false;
}
}
}
I get this error code, in fact I get an error code for both of the get methods:
error:
cannot find symbol
return ((column == other.getColumn()) && (row == other.getRow()));
^
symbol: method getRow()
location: variable other of type Object
should be
Object doesn’t contain
getColumn()andgetRow()methods, it is position, so you need to use position there.