Why is the parameter of the equals () method of the new class of type Object?

I am re-reading the creation of Java Stuart Reges programs and notice that I do not quite understand. This applies to method overloading equals()in any new class. Suppose we define a class as such:

public Point{  
   private int x;
   private int y;

   public Point(int x, int y){
       this.x = x;
       this.y = y;
   }

   public getX(){
       return this.x;
   }

   public getY(){
       return this.y;
   }
}

The book shows that at any time when we define a new class, the equals () method that we define for the new class should be written as such:

public boolean equals(Object o) {
     if (o instanceof Point) {
          Point other = (Point) o;
          return x == other.x && y == other.y;
      } else { 
          return false;
      }
}

equals "Object", "Point"? , equals equals Object, ( , , ). , , , ...

String equals(), Point, String, false. equals() ( ) , ?

+4
4

. , equals , .

, ArrayList ( AbstractList). List , . , , ArrayList List, a LinkedList, ArrayList List.

+5

:

public class BoatWeight {
    float weightTonnes;
    public float getWeightTonnes() {
        return weightTonnes;
    }
}

public class CarWeight {
    int weightLb;
    public int getWeightLb() {
        return weightLb;
    }
}

. :

public class CarWeight {
    ...
    public boolean equals(Object o) {
         if (o instanceof BoatWeight) {
             final int boatWeightLb =
                 WeightConverter.tonnesToLb(o.getWeightTonnes());
             return  boatWeightLb == this.getWeightLb();
         } else if (o instanceof CarWeight) {
             return this.getWeightLb() == o.getWeightLb();
         }
    }
    ...
}

, , , , equals(). , ? . - , ...

+1

,

boolean equals(Object o){}

Object. , " ".

0

- , , , ..:

@Override
public boolean equals(Object o) {
  return (o instanceof Point) ? equals((Point) o) : false;
}

public boolean equals(Point other) {
  return x == other.x && y == other.y;
}
0

Source: https://habr.com/ru/post/1672649/


All Articles