I'm trying to override the equals() method that takes an Object as input.
I have the following classes in the same package
public class Herd{
int count;
boolean exists;
In the class that is overriding the method, I am trying to compare whether the Object matches the variable in position, rank, and if they are the same Class
public class Animal{
private Herd lot;
private int rank;
public boolean equals(Object animl) {
if(this.getClass() == animl.getClass() && this.rank == animl.rank && this.lot == animl.**lot**) {
return true;
}
return false;
}
I know that to compare I will use an object that has all these parameters, however, in the Animal class itself it says
for animl.rank "rank cannot be resolved or is not a field"
for animl.lot "lot cannot be resolved or is not a field"
I tried downcasting i.e. (... == (Animal) animl.rank) but it gives me an incompatible operand types error. I also try casting the rank into an int, but it gives me the above problem.
Any help appreciated.