3

Based on many tutorials that I have read, the following is the definition of Polymorphism:

Polymorphism is the ability of an object to take on many forms

Now let's assume that we have an Animal parent class, and a Dog and a Cat child classes.

Is the above Polymorphism definition means that an Animal variable can have many forms in the sense that an Animal variable can be an Animal or it can be a Dog or it can be a Cat, or does it mean something else?

  • 3
    This is just a metapher - a simplified image or comparison to make it easier to remember what polymorphism really is, without describing it in a mathematically or formally correct way. I would not overinterpret this. – Doc Brown May 14 at 21:28
  • Your confusion may be in the word ability which suggest Animal has magic powers that allows it to transform into either a dog or a cat. It is none of that, objects are passive as far as their types are concerned. The types are immutable. Polymorphism says they may respond differently depending on their type. You see an animal, you tell it to speak, the animal says "Woof!", now YOU know it's a dog. – Martin Maat May 15 at 4:27
2

There is a dual:

  1. that one object can be taken as different forms — a Dog instance can be taken both as Dog and as Animal

  2. that a variable can refer to instances of different types as long as they conform — an Animal variable can refer to a Dog or Cat instance


Polymorphism is the ability of an object to take on many forms

If you ask me, this statement speaks more to (1).

However, both of these things (1&2) happen together in polymorphism; having one implies the other.

Is the above Polymorphism definition means that an Animal variable can have many forms in the sense that an Animal variable can be an Animal or it can be a Dog or it can be a Cat

Yes!

| improve this answer | |
  • I agree that 1 is the key definition here. It becomes even clearer when you consider the case of a type that implements multiple interfaces. A DrugSnifferDog that implements both Animal and PoliceOfficer (eats doughnuts, catches criminals, ...) can take the form of either alone. – combinatorics May 15 at 7:40
2

You don’t know.

Oh sure, semantically we know a dog is an animal but that isn’t what you’re asking.

For an animal variable to be an animal, not just something that is-a animal, it must be concrete, not abstract. That means it must, on it’s own, define everything that animal is and does.

That doesn’t mean some dog can’t come along and redefine that while also being an animal. Abstract or concrete can be redefined.

But when we talk to something that we know only as animal we don’t know if that’s really what we’re talking to. All we know is how to talk to it. Weird as it is, that’s actually a good thing. It means what it is can be decided elsewhere.

The ability to deal with the “form” being decided elsewhere is what we call polymorphism.

| improve this answer | |
0

A better definition is

polymorphism is the provision of a single interface to entities of different types

Rather than one object taking on many forms, it's about many objects taking on one consistent form. In my mind anyway. Both dogs and cats can be interacted with as animals, without the need to know that they're different or even that there is such a thing as a dog or a cat.

I don't think the quote is great for explaining polymorphism tbh.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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