Inheritance and abstract members
Inheritance is the other half of the trait mechanism: a class may extend one class, inherit its concrete members, and be required by an abstract member to supply the rest. An abstract member is a promise the base makes to its callers and a duty the subclass has to fulfil; the compiler checks every subclass.
An abstract class
Shape cannot be instantiated: it declares area and name without a body. The two subclasses supply both, and the base writes a toString that uses them:
shapes.scala
abstract class Shape:
def area: Double
def name: String
override def toString: String = f"$name ($area%.2f)"
class Circle(val radius: Double) extends Shape:
val name = "circle"
def area: Double = math.Pi * radius * radius
class Rectangle(val width: Double, val height: Double) extends Shape:
val name = "rectangle"
def area: Double = width * height
@main def shapes(): Unit =
val shapes: List[Shape] = List(new Circle(1), new Rectangle(2, 3))
shapes.foreach(println)
println(shapes.map(_.area).sum)
scala-cli run --server=false shapes.scala
circle (3.14)
rectangle (6.00)
9.141592653589793
The list is a List[Shape], so the loop calls the base type and the subclass decides the result: that is the substitution the type system guarantees. The override on toString is required because every class inherits one from Any; a member that overrides nothing is a compile error, which is how a typo in a method name is caught.
An abstract val
A member may be an abstract val, and the subclass supplies it as a val or as a constructor parameter. The base uses it in a concrete method, so the value is fixed before any method runs:
abstract_val.scala
abstract class Animal:
val sound: String
def speak: String = s"the animal says $sound"
class Dog extends Animal:
override val sound: String = "woof"
class Cat extends Animal:
override val sound: String = "meow"
@main def animals(): Unit =
val animals: List[Animal] = List(new Dog, new Cat)
animals.foreach(animal => println(animal.speak))
scala-cli run --server=false abstract_val.scala
the animal says woof
the animal says meow
A val that the base uses during construction is the one risky shape of inheritance: if the subclass initialises it from a def or a var, the base may read it before the value is there. This example initialises it from a literal, which is safe, and a lazy val or a method is the fix when a value has to be computed.
final and sealed
A member marked final cannot be overridden and a class marked final cannot be extended; both are promises to a caller that the behaviour it reads is the behaviour it gets. A sealed class or trait is the opposite promise: every subclass is in the same file, which is what lets a match over it be checked for completeness. The sealed hierarchies lesson takes that one.
Related
- Traits and linearization: mixing behaviour in instead of extending a class.
- Sealed hierarchies: the exhaustive match that a sealed base makes possible.
- Enums: a sealed hierarchy with less syntax.
- Classes and constructors: the subclass constructor.
Автор статьи: Arthur Isaev