The instanceof
operator
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 |
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.print(r1.equals("Hi"));//false: Differing classes Rectangle and String. System.out.print(r1.equals(r2)); //false: Differing width and height. System.out.print(r3.equals(r1)); //true: Two rectangles having identical // (x|y), width and height. System.out.print(r1.equals(c)); //false: Differing classes Rectangle and Circle. System.out.print(c.equals(c)); //true: Object equal to itself.
Two Shape
instances shall be considered
equal if:
-
Both instances are of common type i.e. either
Rectangle
orCircle
. -
Their center coordinates match within a threshold of .
-
width
andheight
orradius
match within a threshold of .
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;
}
...
public class Rectangle extends Shape { @Override public boolean equals(Object o) { if (o instanceof Rectangle r) { final Rectangle oRectangle = (Rectangle) o; // Cast is «legal» return super.equalCenter(r) && Math.abs(r.width- width) + Math.abs(r.height- height) < 1.E-15; } return false; } ...
For o == null
the expression o instanceof
Rectangle
evaluates to false
.
public class Circle extends Shape { @Override public boolean equals(final Object o) { if (o instanceof Circle c){ return super.equalCenter(c) && Math.abs(c.radius - radius) < 1.E-15; } return false; } ...
|
r1.equals(r2): false r1.equals(r3): true c.equals(r1): false |