Created
October 27, 2014 03:52
-
-
Save rtoal/d5e3ae7462da87462d92 to your computer and use it in GitHub Desktop.
An example of inheritance and polymorphism in Scala
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
abstract class Animal(name: String) { | |
def speak = name + " says " + sound | |
def sound: String | |
} | |
class Cow(name: String) extends Animal(name) { | |
override def sound() = "moooooo" | |
} | |
class Horse(name: String) extends Animal(name) { | |
override def sound() = "neigh" | |
} | |
class Sheep(name: String) extends Animal(name) { | |
override def sound() = "baaaa" | |
} | |
var h = new Horse("CJ") | |
println(h.speak) | |
var c = new Cow("Bessie") | |
println(c.speak) | |
println(new Sheep("Little Lamb").speak) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment