Re: What is polymorphism?
Seigfried <Seigfried@msn.com> wrote:
I have to write a paper about object oriented programming and I'm doing
some reading to make sure I understand it. In a book I'm reading,
however, polymorphism is defined as:
"the ability of two different objects to respond to the same request
message in their own unique way"
I am not quite sure what you mean by "request message", but in this
context I am assuming you mean a function call.
For example, I'll use the canonical polymorphism example of shapes.
Suppose we have an abstract class Shape:
class Shape {
public:
// pure virtual function has no implementation and must be
// overridden by derived classes, since there is not really a
// generic algorithm to draw an arbitrary shape
virtual void draw() = 0;
// virtual destructor is needed so that derived classes will be
// destroyed correctly when being deleted through a base class
// pointer
virtual ~Shape() { }
};
Then we have concrete specific shapes:
class Square : public Shape {
public:
virtual void draw() { /* draw a square */ }
};
class Circle : public Shape {
public:
virtual void draw() { /* draw a circle */ }
};
Then, we can instantiate two different objects, and they will respond to
the same request (draw) in their own unique way:
Shape& s = Square();
Shape& c = Circle();
// Draw a square
s.draw();
// Draw a circle
c.draw();
I thought that it was:
"the ability of same object to respond to different messages in
unique ways"
I would interpret this more like function overloading, but see below.
class Drawer {
public:
void draw(Square s) { /* draw a square */ }
void draw(Circle c) { /* draw a circle */ }
};
Drawer d;
Square s;
Circle c;
// Draw a square
d.draw(s);
// Draw a circle
d.draw(c);
However, this has the drawback (no pun intended) that the Drawer class
must know how to draw each kind of shape. If you decide to make a new
shape (e.g., Triangle), then you also need to update the Drawer class.
However, using polymorphism as described from the first example, we
could reimplement the Drawer class as such:
class Drawer {
public:
void draw(Shape& s) { s.draw(); }
};
All classes that derive from Shape must implement the draw() function,
so the above will work. Since draw() is virtual and we are calling it
through a reference (or a pointer), then the virtual dispatch mechanism
will kick in, and the draw() function that actually gets executed will
depend on the specific type of Shape that it is at run-time.
Drawer d;
Square s;
Circle c;
// Draw a square
d.draw(s);
// Draw a circle
d.draw(c);
Now, when we create the new Triangle type, we just have to implement
Triangle's draw() function, and it will work with the existing Drawer
class with no changes needed.
I am guessing that this last example could be interpreted as "the same
object responding to different messages in unique ways".
The examples in the book that follow seem to support my understanding.
Is this an error in the book or am I not understanding it?
--
Marcus Kwok
Replace 'invalid' with 'net' to reply