Polymorphism
Polymorphism allows algorithms to be specified in terms of the
most general (and/or reasonable) object type
void processShape(Shape s) {
s.setColor(Color.red);
// ... do stuff
s.draw();
}
Circle c = new Circle(x, y, r);
Point2D v1 = new Point2D(x1, y1);
Point2D v2 = new Point2D(x2, y2);
Point2D v3 = new Point2D(x3, y3);
Triangle t = new Triangle(v1, v2, v3);
Point2D c1 = new Point2D(u1, v1);
Point2D c2 = new Point2D(u2, v2);
Triangle r = new Rectangle(c1, c2);
processShape(c);
processShape(t);
processShape(r);
|