The instanceof operator

Figure 452. Defeating polymorphism Slide presentation Create comment in forum
public static void main(String[] args) {
  final Shape[] shapes = {
    new Circle(1, 1, 2.),
    new Rectangle(1, -1, 2., 3.)};
  print(shapes);
}
static void print(final Shape[] shapes) {
  for (final Shape s : shapes) {
    if (s instanceof Rectangle) {
      System.out.println("Type Rectangle");
    } else if (s instanceof Circle) {
      System.out.println("Type Circle");
    }
  }
}
Type Circle
Type Rectangle

Figure 453. Defining equals(...): Expectations Slide presentation Create comment in forum
Rectangle r1 = new Rectangle(1, 2, 5, 4),
          r2 = new Rectangle(1, 2, 1, 7),
          r3 = new Rectangle(1, 2, 5, 4);

Circle c = new Circle(-2, 3, 5);

System.out.println(r1.equals("Hello")); // false: Differing classes Rectangle and String.
System.out.println(r1.equals(r2));      // false: Differing width and height.
System.out.println(r3.equals(r1));      // true:  Two rectangles, 
                                        // identical (x|y), width and height.
System.out.println(r1.equals(c));  // false: Differing classes Rectangle and Circle.
System.out.println(c.equals(c));   // true: Object equal to itself.

Figure 454. Defining equals(...) of Shape instances Slide presentation Create comment in forum

Two Shape instances shall be considered equal if:

  • Both instances are of common type i.e. either Rectangle or Circle.

  • Their center coordinates match within a threshold of 10 - 15 .

  • width and height or radius match within a threshold of 10 - 15 .


Figure 455. Comparing center coordinates Slide presentation Create comment in forum
public abstract class Shape {
  private double x, y;

  protected boolean equalCenter(final Shape o) {
      return Math.abs(o.x - x) + Math.abs(o.y - y) < 1.E-15;
  }
...

Figure 456. Implementing Rectangle.equals() Slide presentation Create comment in forum
public class Rectangle extends Shape {
    @Override public boolean equals(Object o) {
    if (o instanceof Rectangle){
      final Rectangle oRectangle = (Rectangle) o;
      return super.equalCenter(oRectangle) &&
          Math.abs(oRectangle.width- width) +
          Math.abs(oRectangle.height- height) < 1.E-15;
    }
    return false;
  }
 ...

if o == null then o instanceof Rectangle evaluates to false.


Figure 457. Implementing Circle.equals() Slide presentation Create comment in forum
public class Circle extends Shape {
  @Override public boolean equals(final Object o) {
    if (o instanceof Circle){
      final Circle oCircle = (Circle) o;
      return super.equalCenter(o) &&
        Math.abs(oCircle.radius - radius) < 1.E-15;
    }
    return false;
  } ...

Figure 458. Testing equality of Shape objects Slide presentation Create comment in forum
final Rectangle
    r1 = new Rectangle(2, 3, 1,4),
    r2 = new Rectangle(2, 3, 2,8),
    r3 = new Rectangle(2, 3, 1,4);

final Circle c = new Circle(2,3, 7);

System.out.println("r1.equals(r2): " + r1.equals(r2));
System.out.println("r1.equals(r3): " + r1.equals(r3));
System.out.println("c.equals(r1): " + c.equals(r1));
r1.equals(r2): false
r1.equals(r3): true
c.equals(r1): false