Concepts / Objects and Messages

Objects and Messages

Polymorphism concerns different responses to one shared message.

  • Programming

A Message Sent Twice

Imagine sending the same message to two different objects. The message is identical, word for word, but the objects do not have to respond in the same way. One object may produce one kind of result while another produces a very different result. This is the central idea behind objects and messages in programming, and it is the starting point for understanding polymorphism.

In this article you will follow one shared message through a few different receivers, then see how objects are grouped into classes, and finally look at the cases where objects that seem alike still belong to different classes.

Predicting the Response

What do you think happens?

Two different objects both receive the message print. Does the message name alone decide what each object does?

  • Yes, the name print always produces the same result everywhere

  • No, the receiving object determines the response

Reveal answer
Answer:

No, the receiving object determines the response

The source states that the important comparison is not between the two message names, since both messages are print. The comparison is between the receiving objects and the responses they produce. The same message name can therefore lead to different results depending on which object receives it.

Polymorphism: Different Responses to One Message

Definition

Polymorphism is the ability of different objects to respond differently to the same message. The message stays identical, and the difference lies in the receiving object and the response it produces.

It helps to separate two things that are easy to blur together. The message is the request being sent, such as print. The response is what the receiving object does when it gets that request. Polymorphism describes the case where one shared message leads to different responses. It does not describe a change in the message itself.

printthe shared messageObject Areceives printResponse from Aone kind of resultObject Breceives printResponse from Ba very different resultsent tosent toproducesproduces
When the same message is sent to several different objects, how does each object's response differ while the message stays identical?
Worked example

Tracing the same message through two objects

The message print is sent to Object A and then to Object B. Trace what the learner should compare.

  1. 1
    Identify the message

    Both sends use the identical message name, print. Nothing about the message has changed between the two sends.

  2. 2
    Identify the receiver

    The first send goes to Object A and the second goes to Object B. These are different objects.

  3. 3
    Compare the responses

    Each object produces its own response. Object A produces one kind of result and Object B produces a very different one.

  4. 4
    Locate the difference

    The difference comes from the receiving object, not from the message. That is what polymorphism describes.

Result

The same message produced different responses because the receiving objects differ. This is polymorphism.

Grouping Objects into Classes

Objects are gathered into classes. According to the source, classes group objects by their data characteristics and their message behavior. An object's data characteristics are the information it holds, and its message behavior is the way it responds to the messages it receives. A class collects objects that share these relevant features.

A class is best understood as a template. The source explains that a class serves as a template from which objects are created. Objects created from the same class share the class's relevant characteristics, so the template determines what those objects have in common.

Similar Objects, Different Classes

A natural assumption is that objects that look alike or behave alike must belong to the same class. The source warns against this. Similarity between objects does not automatically mean that they belong to the same class. Two objects can share some surface features or behavior and still be grouped under different classes, because classes are defined by their relevant data characteristics and message behavior, not by resemblance alone.

Consider a generated illustration. Two objects both hold a single piece of text and both respond to print. They look similar in these respects. If their class definitions differ in other relevant characteristics or in how they respond to messages, they belong to different classes even though they resemble each other.

Common Misunderstandings About Classes and Polymorphism

  • Common mistake

    Thinking polymorphism means the message name changes

    Why it fails

    The source is explicit that the message is identical. Polymorphism concerns the response, which depends on the receiving object.

    Do this instead

    Keep the message fixed and compare the receiving objects and their responses.

  • Common mistake

    Treating a class as a label added after looking at objects

    Why it fails

    In actual programming the class is created first and objects are then created from it as a template.

    Do this instead

    Think of the class as the template that comes before the objects.

  • Common mistake

    Assuming that similar-looking objects must share a class

    Why it fails

    Similarity does not automatically mean two objects belong to the same class.

    Do this instead

    Check the relevant data characteristics and message behavior that define the class.

  • Common mistake

    Confusing the class with one of the objects created from it

Trying the Idea Yourself

EASY

Two objects, X and Y, both receive the message print. X responds with one kind of result and Y responds with a very different one. Explain, in your own words, what has stayed the same across the two sends and what has changed, and say which part of this situation polymorphism describes.

Hints
  • Identify the message first and confirm whether it differs between the two sends
  • Then identify what differs: the receiving object and the response it produces
  • Polymorphism names the difference in response to a shared message
MEDIUM

Suppose two objects share a message, but one was created from a class and the other was not. Explain why the question of which class each object belongs to cannot be answered by looking only at how similar the two objects appear.

Hints
  • Recall that similarity between objects does not automatically mean they belong to the same class
  • Think about what a class is defined by: data characteristics and message behavior

Key Takeaways

  • Polymorphism is the ability of different objects to respond differently to one shared message; the difference lies in the receiving objects and their responses.
  • Classes group objects by their data characteristics and message behavior, and a class serves as a template from which objects are created.
  • In actual programming the class is created first, and the objects are then created from it, sharing the class's relevant characteristics.
  • Similarity between objects does not automatically mean they belong to the same class; class membership depends on the relevant characteristics that define the class.