Object-oriented – polymorphism if you can already have methods that are the same defined in different types

object-orientedpolymorphism

I often read definitions for Polymorphism such as the following:

Polymorphism is the ability to have objects of different types
understanding the same message

But the above definition also apply if we don't use Polymorphism, for example if we have an object of type Circle with a method draw(), and another object of type Rectangle with a method draw(), we can do:

circle1.draw();
rectangle1.draw();

So circle1 and rectangle1 understood the same message draw() without using Polymorphism!

Am I missing something?

Best Answer

In your example, you don't really show the same message, you show two different messages that happen to have the same name. Polymorphism requires that the sender of a message can send it without knowing the exact recipient. Without seeing evidence that the caller can do something like shape.draw() without knowing whether shape contains a circle or a rectangle, you may or may not have actual polymorphism. They could be as unrelated as circle.draw() and weapon.draw().

They don't necessarily have to both implement the same nominal interface. The language could support structural typing or compile-time templating and it would still be called polymorphism. As long as the caller doesn't care who the callee is.

Related Topic