Skip to content

Instantly share code, notes, and snippets.

@rtoal
Created October 27, 2014 03:46
Show Gist options
  • Save rtoal/c254a6e2d8c02ae68864 to your computer and use it in GitHub Desktop.
Save rtoal/c254a6e2d8c02ae68864 to your computer and use it in GitHub Desktop.
An example of inheritance and polymorphism in CoffeeScript
# A typical example of inheritance
class Animal
constructor: (@name) ->
speak: ->
"#{@name} says #{this.sound()}"
class Cow extends Animal
sound: -> "moo"
class Horse extends Animal
sound: -> "neigh"
class Sheep extends Animal
sound: -> "baaaaa"
s = new Horse "CJ"
console.log s.speak()
c = new Cow "Bessie"
console.log c.speak()
console.log new Sheep("Little Lamb").speak()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment