September 2018
Intermediate to advanced
398 pages
9h 43m
English
When you derive a class, you can override the members of the superclass to provide a different implementation. Here is an example that you can retype in a new worksheet:
class Shape(val x: Int, val y: Int) { def description: String = s"Shape at (" + x + "," + y + ")"}class Rectangle(x: Int, y: Int, val width: Int, val height: Int) extends Shape(x, y) { override def description: String = { super.description + s" - Rectangle " + width + " * " + height }}val rect = new Rectangle(x = 0, y = 3, width = 3, height = 2)rect.description
When you run the worksheet, it evaluates and prints the following description on the right-hand side:
res0: String = Shape at (0,3) - Rectangle 3 * 2
We defined a method description on the class ...
Read now
Unlock full access