Skip to content

Instantly share code, notes, and snippets.

@vvviiimmm
Created February 18, 2017 09:50
Show Gist options
  • Select an option

  • Save vvviiimmm/810356bc5727ade79fc174496c7a6466 to your computer and use it in GitHub Desktop.

Select an option

Save vvviiimmm/810356bc5727ade79fc174496c7a6466 to your computer and use it in GitHub Desktop.
// Our generic interface
trait Animal {
def voice: String
}
// Implementation 1
class Dog extends Animal {
override def voice: String = "woof!"
}
// Implementation 2
class Cat extends Animal {
override def voice: String = "meow!"
}
// Generic function
def say(animal: Animal) = println(animal.voice)
// Usage
say(new Dog) // prints "woof!"
say(new Cat) // prints "meow!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment