I'm trying to implement an extension of the equals method in Java. Problem is, for one reason or the other it gives out the wrong response.
public class Cordes{
private static double x;
private static double y;
public Cordes(double x, double y) {
this.x = x;
this.y = y;
}
public boolean equals(Object somethingelse) {
if (this == somethingelse) {
return true;
}
if (somethingelse instanceof Cordes) {
Cordes theother = (Cordes) somethingelse;
return (this.x == theother.x) && (this.y == theother.y);
}
return false;
}
public static void main(String[] args) {
Cordes p = new Cordes(2.0, 4.0);
Cordes p2 = new Cordes(3.0, 4.0);
System.out.println(p.equals(p2));
}
So this comes out as true, even though it should obviously be false. I'm not sure what to try here, the first part of the code tries to match the memory address, which should be false. The other if statement tries to see if its a part of our class, if it is, it will try and see if these coordinates are equal which should return false.
In addition to this, I have a small side question: When we are casting Cordes on "somethingelse", what does this actually mean, isn't other object already an object, why do we have to cast it, and why does that action enable us to go "theother.x / theother.y".
Thanks in advance!