Edit Rename Changes History Upload Download Back to Top

Quick definition of Polymorphism

There is very little new under the sun. In order to hide this fact people in all professions, including computer science, invent new words for old concepts.

Polymorphism comes from Greek and means "many shapes." As used by object oriented programmers a method can be implemented differently in different classes based on the needs of that specific class.

A really simple and cool example of this exists in all Smalltalk images. All objects know how to test themselves to see if they are the nil object.

Before you start thinking polymorphically, you would be tempted to write a method such as isNil and notNil as follows and let all objects in the system inherit from it:

isNil
    ^self == nil

notNil
    ^self ~~ nil
But once you realize that all objects that are not the nil object (there is only one in the system) answer false for isNil and true for notNil, and that the nil object is the only object that answers true for isNil and false for notNil, it becomes more desirable to be able to write those methods to just return true or false.

All objects inherit from class Object. The nil object is the only instance of UndefinedObject, which is a descendent of Object. So, we can write the following in Object:

isNil
    ^false

notNil
    ^true
and then the following in UndefinedObject:
isNil
    ^true

notNil
    ^false
Back to Troy's Smalltalk Quickstart Notes.


Edit Rename Changes History Upload Download Back to Top