Polymorphism

A Definition

The term Polymorphism means 'many forms'. Appropriately, and perhaps confusingly, it's meaning takes many forms, notably in computer science and biology. We're interested in object-oriented computer software so we'll leave the other definitions to Wikipedia. Of course in object-oriented software there is more than one kind of polymorphism...

Overriding

We're particularly interested in substitution (swapping one kind of object for another), and the ability to plug in new components with new behaviour (creating new kinds of object) in the future. That means we're interested in having many forms of object. And in computer science we use the specific term 'type'. But objects themselves aren't polymorphic. An object has only one type. Of course a object of a specific type can belong to more than one class (interfaces, multiple inheritance); this is known as allomorphism.

If objects don't have many forms do classes? Well, they define many types, so you might be forgiven for thinking so. But a class is its types, that is all of its subclasses and their subclasses, recursively. We can't talk about a class having many forms as it is all of its forms! (Although templates and generics do give us polymorphic classes).

In fact it's the variable, the reference to the object, that is polymorphic. It can hold many forms of object.

Polymorphic variables on their own don't quite get us what we want. On its own this would allow new objects be substituted, but wouldn't allow their behaviour to change. Overriding is what allows a subclass to replace methods on the superclass with their own behaviour. If you couldn't override the methods then subclasses would behave in the same way. So overriding itself isn't polymorphism, it's just what makes it so useful.

Overloading

Methods can be polymorphic. There can be more than one form of a method on the same class. When a method is called the actual method invoked depends on the type of arguments supplied.

Generics

Parametric-polymorphism. Here the classes are polymorphic, there can be many forms of the class depending on the parameter type.