public class FigureTesterV2Instanceof {
    public static void main(String[] args) {
        Point p1 = new Point(3, 2);
        Point p2 = new Point(-1, -1);
        Point p3 = new Point(0, 0);
        int radius = 4;
        Figure[] myFigs = new Figure[3];
        myFigs[0] = new Rectangle(p1, p2);
        myFigs[1] = new Circle(p1, radius);
        myFigs[2] = new Triangle(p1, p2, p3);
        for (int i = 0; i < myFigs.length; i++) {
            System.out.print("Figure " + i + ": ");
            System.out.println(myFigs[i].toString());
            System.out.print("Area: ");
            System.out.println(myFigs[i].calculateArea());
            System.out.print("Perimeter: ");
            System.out.println(myFigs[i].calculatePerimeter());
            System.out.print("instanceof Circle? ");
            System.out.println(myFigs[i] instanceof Circle);
            System.out.print("instanceof Figure? ");
            System.out.println(myFigs[i] instanceof Figure);
            System.out.print("getClass() returns: \"");
            System.out.print(myFigs[i].getClass());
            System.out.println("\"");
            if (myFigs[i] instanceof Triangle) {
                Triangle t = (Triangle) myFigs[i];
                t.printSideLengths();
                // Equivalente a:
                // ((Triangle) myFigs[i]).printSideLengths();
                // El casting es necesario porque printSideLenghts()
                // no es un metodo de Figure
            }
            // Siempre que podamos utilizaremos instanceof
            // en lugar de getClass() porque asi nos olvidamos del package
            System.out.println("========================================");
        }
    }
}
