1

In this question, the asker of the question shows the following example (no Polymorphism is used):

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

And the highest voted answer says the following:

In your example, you don't really show the same message, you show two different messages that happen to have the same name.

I don't think that the above statement is correct, I think that we have only one message and not two messages, and this one message is move(). But if I'm wrong, then how can I make sense of the idea that two unique messages can share the same name?

3 Answers 3

5

What the highest voter mean, is that the example doesn’t show polymorphism: it only suggests it. You assume there is some polymorphism because you infer from the examples that it’s about drawing shape.

Let’s make this clearer with another example:

circle1.draw();    // you ask the circle to draw itself
robot2.draw();   // you ask your robot commander module to make the robot  draw something 
cardset.draw();  // you draw a card. Other meaning of "to draw"

Here we have 3 different messages, each having a completely different meaning for its receiver. The only thing in common is that it’s the same name.

The situation would be different if Circle and Rectangle would implement a Shape interface (or inherit from a dame superclass). If the interface defines a message draw(), the other shapes would implement their own behavior for the same message:

Shape s=getRandomSquareOrRectangle();
s.draw();
4
  • "Here we have 3 different messages" But are they really three different messages? I mean if you sent the same HTTP request message to three different HTTP servers, the HTTP request message is still one message, yes it was sent to three different receivers, but the message is still one message and not three. Same thing with the move() message, yes the move() message was sent to three different objects, but it is still the same message. Jul 9, 2020 at 14:24
  • @user4582812 I answered in the context of the original question, that is OOP and polymorphism, and HTTP alone offers neither of the two. But I like your provocative question because I agree that there is some ambiguity in the most voted answer which I interpret here. In fact it depends on the implementation mechanisms of the language infrastructure: My answer here is valid for strongly typed languages. In C++ for example, a polymorphic message corresponds in practice more or less to an offset in a vtable. This offset in nameless. In other languages it’s a selector or an id (unique number).
    – Christophe
    Jul 9, 2020 at 14:35
  • 1
    @user4582812 So it’s possible that some dynamically typed languages uses message passing by name (or a hash of the name to limit overhead). In such an environment you’d be completely right: the dynamic typing would allow you to send the message to whoever you want, and the receiver could then react to the same message (this time) with completely different behavior.
    – Christophe
    Jul 9, 2020 at 14:40
  • They are different messages. Remember they could have different return types and parameter types.
    – gnasher729
    Jul 9, 2020 at 17:49
1

The bottom line is that we cannot know without seeing more, in particular, we need to know:

  • the declaration/types of these variables,
  • the declaration of the types themselves,
  • the programming language since OOP is implemented with different rules in each one!

Let's assume languages like C++, Java, C#.

These two names could be different and unrelated — in OOP, we use both namespaces and classes to prevent naming conflicts.  Name collisions are a very real problem especially with extensive libraries, developed by multiple third parties.  Languages without namespaces and classes for scoping names suffer more from name collisions, and resort then to longer names, like including the package, class or interface name with the method, such as xyz_draw, pdq_draw...  In the above named OOP languages, it is acceptable to use the same name in different contexts to mean different things — we do not consider them related merely for having the same name.

Or these two names could be in a polymorphic relation — if they are the same name because they are defined in the same interface or base class.

Fundamentally, the code sample is just too small to say: the question is vague or ambiguous.

Technically, we don't even know that there are multiple types involved here due to the missing information.  Let's play devil's advocate for a second and imagine the following:

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

In this case, there is only one type, and the draw method is the same method, and this is not an example of polymorphism.  This is just to show that we need to know the types of the variables (and the declarations of those types as well).

If we were talking about JavaScript or perhaps the original Objective-C, on the other hand, these do dispatch by names not necessarily organized by classes and namespaces, so the same name can be sent to any object, and care is taken with names to avoid name collisions.  If all we have is the name, we might tend to assume the same name used in different contexts are related.

1

Just because one thing has the same name as another thing doesn't mean they are the same. For example, there are thousands of people that share my name but they are not me. Any common word in English has multiple meanings.

If someone says 'duck!' it could mean you should lower your head to avoid being hit by something or maybe they saw an animal of that species. It depends on the context. If we follow your reasoning, both of these are the 'same'. But it's pretty obvious that the meaning of the message is different despite the literal message content being the same.

Consider the binary value: 110000. What does it mean? If we understand this to be a binary integer, it's thirty. However, it we consider it as an ascii character value, it's '0'. If we were to interpret it as EBCDIC value, it has no meaning. One value, unlimited possible interpretations. So again, what does this value mean? The answer is that without a context to interpret the value, it has no specific meaning. If I open a file with an ascii text editor, it will treat this as '0'. But if the intention was to communicate the value 30, it doesn't magically become the letter character '0', you've simple misinterpreted the message by using an incorrect context for evaluating it.

The same thing goes here. If you send a message and it's interpreted by the receiver differently that what you meant, it doesn't change your meaning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.