Overriding equals() and hashCode()

Figure 392. Shape.equals() Slide presentation Create comment in forum
public abstract class Shape {
  ...
  @Override  public boolean equals(final Object o) {
    if (o instanceof Shape ) {
      final Shape other = (Shape) o; 
      return x == other.x && y == other.y; 
    } else {
      return false; 
    } ...

Promise: The current method overrides a superclass method.

Other instance is a Shape object?

Casting other object to class Shape.

Return true if and only if both center coordinate pairs are equal.

Other object distinct from class Shape.


Figure 393. Rectangle.equals() Slide presentation Create comment in forum
public class Rectangle extends Shape {
  ...
  @Override public boolean equals(final Object o) {
    if (o instanceof Rectangle) {
      final Rectangle other = (Rectangle) o;
      return super.equals(o)  &&  width == other.width && height == other.height ;
    } else {
      return false;
    } ...

Including superclass method Shape.equals().

Return true if and only if both width and height pairs are equal.