class Point
{
// Instance fields.
real x;
real y;
// Constructor to initialize fields x and y.
void new(real _x, real _y)
{
x = _x;
y = _y;
}
void write()
{
info("(" + any2Str(x) + ", " + any2Str(y) + ")");
}
}
class ThreePoint extends Point
{
// Additional instance fields z. Fields x and y are inherited.
real z;
// Constructor is overridden to initialize z.
void new(real _x, real _y, real _z)
{
// Initialize the fields.
super(_x, _y);
z = _z;
}
void write()
{
info("(" + any2Str(x) + ", " + any2Str(y) + ", " + any2Str(z) + ")");
}
}
// Code that creates Point objects and calls the write method.
Point point2 = new Point(1.0, 2.0);
Point point3 = new ThreePoint(3.0, 4.0, 5.0);
point2.write();
// Output is "(1.0, 2.0)".
point3.write();
// Output is "(3.0, 4.0, 5.0)".