Product Site

1.8. Polymorphism

Polymorphism gives you a single interface to objects of different types. This example shows instances of classes inheriting from the Collection class, all sharing a common method named put, but with a different implementation each.
Example 1.2. Polymorphism
stem~put("value", "tail")         -- sets a Stem tail to "value"
stringTable~put("value", "index") -- sets a StringTable "index" to "value"
array~put("value", 1)             -- sets Array index 1 to "value"
set~put("value")                  -- makes "value" a member of the Set

The ability to hide the various implementations of a method while leaving the interface the same illustrates polymorphism. On a higher level, polymorphism permits extensive code reuse.