Created
June 1, 2017 13:40
-
-
Save ricsirigu/7fe19bbe8b671b9cb890cf6140f2363e to your computer and use it in GitHub Desktop.
A practical example of subtype polymorphism in Scala
This file contains 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
trait Jsonable{ | |
def toJson: String | |
} | |
case class Picture(name: String, uri: String) extends Jsonable{ | |
override def toJson = s"picture ${name}" | |
} | |
case class Attachment(name: String, uri: String, visible: Boolean) extends Jsonable{ | |
override def toJson = s"attachment ${name}" | |
} | |
val picture: Jsonable = new Picture("mypic", "mypicurl") | |
val attachment: Jsonable = new Attachment("myattachment", "myurl", false) | |
println(picture.toJson) | |
println(attachment.toJson) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment