September 2018
Intermediate to advanced
398 pages
9h 43m
English
Scala classes are extensible. You can extend an existing class to inherit from all its members. If B extends A, we say that B is a subclass of A, a derivation of B, or a specialization of B. A is a superclass of B or a generalization of B.
Let's see how it works in an example. Type the following code in the worksheet:
class Shape(val x: Int, val y: Int) { val isAtOrigin: Boolean = x == 0 && y == 0}class Rectangle(x: Int, y: Int, val width: Int, val height: Int) extends Shape(x, y)class Square(x: Int, y: Int, width: Int) extends Rectangle(x, y, width, width)class Circle(x: Int, y: Int, val radius: Int) extends Shape(x, y)val rect = new Rectangle(x = 0, y = 3, width = 3, height = 2)rect.xrect.yrect.isAtOriginrect.widthrect.height ...
Read now
Unlock full access