Παράδειγμα: σχήματα


class Shape {
    private int x, y;       // Position
    public void setPosition(int px, int py) {
        x = px;
        y = py;
    }

    @Override public String toString() {
        return "Shape(" + x + ", " + y + ")";
    }
}

class Circle extends Shape {
    private int radius;
    public void setRadius(int r) {
        radius = r;
    }

    @Override public String toString() {
        return super.toString() + ": Circle(" + radius + ")";
    }
}

class Rectangle extends Shape {
    private int height, width;

    public void setDimensions(int h, int w) {
        height = h;
        width = w;
    }

    @Override public String toString() {
        return super.toString() + ": Rectangle(" + height + " x " + width + ")";
    }
}

class Test1 {
    static public void main(String args[])
    {
        Circle c = new Circle();
        Rectangle r = new Rectangle();

        r.setPosition(1, 2);
        r.setDimensions(50, 50);
        c.setPosition(3, 4);
        c.setRadius(10);
        System.out.println(r);
        System.out.println(c);
    }
}