8.16.0.2

4.1 Subclasses🔗ℹ

When defining body of a new class, add extends clause followed by the name of an existing class. Instances of the new class, the subclass, will also count as instances of the existing class, the superclass. Since that creates obligations on the superclass, however, a class is final by default, which means that it does not permit subclasses.

class Posn(x, y)

class Posn3D(z):

  extends Posn

class: superclass is final and cannot be extended

To allow subclasses, add a nonfinal clause in a class:

class Posn(x, y):

  nonfinal

class Posn3D(z):

  extends Posn

When a subclass is created, superclass fields are implicitly included before new fields in the subclass’s constructor:

> def p = Posn3D(1, 2, 3)

> p

Posn3D(1, 2, 3)

> p.y

2

> p is_a Posn

#true

> p is_a Posn3D

#true