Last month, I was at StrangeLoop, a conference that draws a lot of functional programmers, and generally very smart people. They gave me the idea for a blog series that details some of the “scary words in programming” (oooOOOooo)! So for the month of October, my weekly post will be about words that scared me when I first heard them (and still kind of scare me!)
(dig it? I made friends with Pixelmator)
The first word is polymorphism.
Polymorphism, besides sounding like a completely made-up word (this is an opinion I have for all the scary words, though), means the ability to reuse a method name (well, technically “a single interface for interacting with,” but let’s call it a method for my brain’s sake) on different types.
For example you can call a method with the same name and have different behaviors happen on different objects. I even asked Corey Haines on the podcast if this is what he meant when he talked about polymorphism in his book (get $5 off using the Turing Incomplete link!). So this episode of Scary Words in Programming is also brought to you by Corey Haines.
Example code in Ruby, since classes are what are kinda types in Ruby, defining a dog and a cat class that both have a ‘sound’ method:
class Cat def sound "meow" end end class Dog def sound "woof" end end c = Cat.new d = Dog.new puts c.sound puts d.sound
Corey did also warn me that probably have different conceptualizations of polymorphism, so if you know more than me, you’re welcome to disagree! But the thing about this example, is I’m performing the “same” interface action on two different types, and different stuff happens because those types have different behaviors. As the caller, I don’t have to care about how each one implements it, so long as it’s available on each type.