Skip to content

Scary words in programming, episode 1: Polymorphism

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!)

scary-words-in-programming-with-pam

(dig it? I made friends with Pixelmator)

The first word is polymorphism.

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.

4 Replies to “Scary words in programming, episode 1: Polymorphism”

  1. Hi,
    So, is inheritance a kind of polymorphism? Like, if I have a method getArea that applies to class “shapes,” and getArea is used by by the “square” and “circle” shapes, is the polymorphism only between square and circle, or between all 3?
    Sorry if this is a garbled question!
    Thanks,
    Kristin

    1. Not quite, because inheritance is a relation between objects and not types. I went to ye olde wikipedia to look up subtyping when Ficarra (another comment) mentioned it, and this was helpful:

      “Subtyping should not be confused with the notion of (class or object) inheritance from object-oriented languages;[1] subtyping is a relation between types (interfaces in object-oriented parlance) whereas inheritance is a relation between implementations stemming from a language feature that allows new objects to be created from existing ones. In a number of object-oriented languages, subtyping is called interface inheritance, with inheritance referred to as implementation inheritance.”

      I think the bit about interface inheritance vs implementation inheritance is the important part. Polymorphism is about sharing the *interface*, like using the same words to call something, but not caring what happens in the middle. That’s Dog vs. Cat sharing an interface. Implementation inheritance is that Cat and Dog are both from Animal, and has to do with creating objects.

  2. The particular form of polymorphism you are talking about here is dynamic dispatch, where the implementation that is used is not decided until runtime. In Java, polymorphism is most frequently achieved through function overloading and subtyping, while in Haskell, polymorphism is most frequently achieved through typeclasses and parameterised types. The reason I feel that people are confused by the term “polymorphism” is that it refers to various kinds of reuse of behaviour through any of these listed methods.

    Hope that helps clear things up a bit. I’m looking forward to the rest of the series!

    1. Yayyyy a Michael Ficarra response! Please respond to every post in this series 🙂 Appreciate the multilingual perspective because I suspected that in Ruby, since it’s dynamically typed, polymorphism looks different than it would in another language.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.