die Polymorphie (pl. -n) – polymorphism
Since the previous one was about object-orientation, I thought might as well continue with the same theme.
The word Polymorphie carries the general meaning of variety or diversity of form (Vielgestaltigkeit, Verschiedengestaltigkeit), but in the context of Computer Science, Wiktionary says that it means the fact that different classes can have methods with the same name. („Der Umstand, dass verschiedene Klassen über Methoden mit demselben Namen verfügen können“)
Hmm, I would say not quite. It’s not only the name.
Just as important is the method signature.
That is, the input and output of the method, that also needs to be the same.
Having multiple methods with the same name but different signatures is simply overloading.
But even that doesn’t really capture what you can do with polymorphism, which is to call a method of a class without really caring about whether it is a parent class or a subclass. You would be calling the more general type of class.
Imagine that we have a dog object.
You can have different kinds of dogs, like Golden Retrievers or Poodles or Chihuahuas. If you had a program that modelled this, you might have each breed of dog as a class and they all inherit from a parent class Dog, since they are all dogs.
Then, let’s say you want a dog to fetch. You would just ask the dog to fetch (in real life) by shouting out “Fetch!”
If this were a program, you would simply call the fetch method that all the classes have to implement. Each dog might have a different response to the command of fetch (child classes can have their own implementations of a method), but they all respond to the command.
Now, that isn’t a perfect analogy by any means, but it was something off the top of my head.
Another example, taken from the cover image of this post, is an animal object.
All animals (almost all) make some sort of noise that we can hear, so you can imagine they have a makeNoise method.
If you were to call this method on on a dog object, you would imagine that it would be along the lines of “Woof!”, while if it was a cat, you would get something like a “Miao!” Both dogs and cats in this case inherit from an animal object, something that will be covered in a future post.
Duden says that the word “polymorph” comes from the Greek: polýmorphos, which is the prefix polýs (many) together with morphḗ (form, shape).
This essentially gives the meaning of something that has many forms.
German also has the word Morphe (die Morphe) which is also derived from morphḗ and carries the meaning of shape. Its synonym is die Gestalt.