Created
February 18, 2017 09:50
-
-
Save vvviiimmm/810356bc5727ade79fc174496c7a6466 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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