Chapter 29: the parent's version on purpose, built

A dispatcher chooses a handler by the run-time type of its argument; an ordinary override chooses by the receiver. Chapter 29 ends on the reverse of both: a subclass that deliberately reaches the PARENT's version with super. Account is open, SavingsAccount extends it, and each override calls the parent's method and adjusts the answer. The IDE writes both classes, their fields, constructors and method signatures; you type what each one does. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-super-call.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

#!ek9
defines module book.vol2.dispatcher.supercall

  defines class

    Account as open
      holder <- String()
      balance <- Money()

      Account() as pure
        ->
          holder as String
          balance as Money

        this.holder :=? String(holder)
        this.balance :=? Money(balance)

      describe() as pure
        <- rtn as String: `${holder} holds ${balance}`

      monthlyCharge() as pure
        <- rtn as Money: 2.50#GBP

      default operator ?

    SavingsAccount extends Account
      rate <- Float()

      SavingsAccount() as pure
        ->
          holder as String
          balance as Money
          rate as Float

        super(holder, balance)
        this.rate :=? Float(rate)

      override describe() as pure
        <- rtn as String: `${super.describe()} at ${rate}%`

      override monthlyCharge() as pure
        <- rtn as Money: super.monthlyCharge() * 0.0

      default operator ?

  defines program

    SuperCalls()
      stdout <- Stdout()

      current <- Account("Ada", 120.00#GBP)
      savings <- SavingsAccount("Bob", 800.00#GBP, 3.5)

      stdout.println(current.describe())
      stdout.println(savings.describe())

      asAccount as Account: savings
      stdout.println(asAccount.describe())
      stdout.println(`charges: ${current.monthlyCharge()} and ${asAccount.monthlyCharge()}`)

//EOF

Step 1: Account — a class

Choose Code > New Construct > Class…

The EK9 IDE once this is done: Choose Code > New Construct > Class…

Account is the class's name. Choose Open under Extensibility - a class is closed to extension unless it says so. Choose Preview.

The EK9 IDE just before choosing Preview: Account is the class's name

The preview shows Account about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Account about to be written

Select the Account line and, above it, type the blank line the book opens its classes with. Saves when typed.

The EK9 IDE once this is done: Select the Account line and, above it, type the blank line the book opens its classes with

Right-click the Account line and choose Add Field…

The EK9 IDE once this is done: Right-click the Account line and choose Add Field…

Type holder <- String() - the field and the value it starts with. Choose OK.

The EK9 IDE just before choosing OK: Type holder <- String() - the field and the value it starts with

The preview shows holder added - and default operator ? with it, Account's first field. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows holder added - and default operator ? with it, Account's first field

Right-click the Account line and choose Add Field…

The EK9 IDE once this is done: Right-click the Account line and choose Add Field…

Type balance <- Money() - the field and the value it starts with. Choose OK.

The EK9 IDE just before choosing OK: Type balance <- Money() - the field and the value it starts with

The preview shows balance added. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows balance added

Right-click the Account line and choose Add Constructor > From All Fields (pure) - a constructor that takes every field.

The EK9 IDE once this is done: Right-click the Account line and choose Add Constructor > From All Fields (pure) - a constructor that takes every field

Select the stub, from Account() as pure down to its last assignment, and type the constructor's whole job as the book writes it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from Account() as pure down to its last assignment, and type the constructor's whole job as the book writes it

Right-click the Account line and choose Add Method…

The EK9 IDE once this is done: Right-click the Account line and choose Add Method…

describe is the method's name. Set Return type to String and tick pure. Choose Apply.

The EK9 IDE just before choosing Apply: describe is the method's name

Select the stub Add Method wrote and type describe's own answer, as the book writes it. Saves when typed.

The EK9 IDE once this is done: Select the stub Add Method wrote and type describe's own answer, as the book writes it

Right-click the Account line and choose Add Method…

The EK9 IDE once this is done: Right-click the Account line and choose Add Method…

monthlyCharge is the method's name. Set Return type to Money and tick pure. Choose Apply.

The EK9 IDE just before choosing Apply: monthlyCharge is the method's name

Select the stub Add Method wrote and type monthlyCharge's own answer, as the book writes it. Saves when typed.

The EK9 IDE once this is done: Select the stub Add Method wrote and type monthlyCharge's own answer, as the book writes it

Step 2: SavingsAccount — a class

Choose Code > New Construct > Class…

The EK9 IDE once this is done: Choose Code > New Construct > Class…

SavingsAccount is the class's name. Type Account into Extends. Choose Preview.

The EK9 IDE just before choosing Preview: SavingsAccount is the class's name

The preview shows SavingsAccount about to be written - and, if Account owes any methods, an override stub for each. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows SavingsAccount about to be written - and, if Account owes any methods, an override stub for each

Right-click the SavingsAccount line and choose Add Field…

The EK9 IDE once this is done: Right-click the SavingsAccount line and choose Add Field…

Type rate <- Float() - the field and the value it starts with. Choose OK.

The EK9 IDE just before choosing OK: Type rate <- Float() - the field and the value it starts with

The preview shows rate added - and default operator ? with it, SavingsAccount's first field. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows rate added - and default operator ? with it, SavingsAccount's first field

Right-click the SavingsAccount line and choose Add Constructor > From All Fields (pure) - a constructor that takes every field.

The EK9 IDE once this is done: Right-click the SavingsAccount line and choose Add Constructor > From All Fields (pure) - a constructor that takes every field

Select the stub, from SavingsAccount() as pure down to its last assignment, and type the constructor's whole job as the book writes it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from SavingsAccount() as pure down to its last assignment, and type the constructor's whole job as the book writes it

Right-click the SavingsAccount line and choose Add Method…

The EK9 IDE once this is done: Right-click the SavingsAccount line and choose Add Method…

describe is the method's name. SavingsAccount replaces its parent's version. Set Return type to String, tick pure and override. Choose Apply.

The EK9 IDE just before choosing Apply: describe is the method's name

Select the stub Add Method wrote and type describe's own answer, as the book writes it. Saves when typed.

The EK9 IDE once this is done: Select the stub Add Method wrote and type describe's own answer, as the book writes it

Right-click the SavingsAccount line and choose Add Method…

The EK9 IDE once this is done: Right-click the SavingsAccount line and choose Add Method…

monthlyCharge is the method's name. Set Return type to Money, tick pure and override. Choose Apply.

The EK9 IDE just before choosing Apply: monthlyCharge is the method's name

Select the stub Add Method wrote and type monthlyCharge's own answer, as the book writes it. Saves when typed.

The EK9 IDE once this is done: Select the stub Add Method wrote and type monthlyCharge's own answer, as the book writes it

Step 3: The program

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

SuperCalls is the program's name. Choose Preview.

The EK9 IDE just before choosing Preview: SuperCalls is the program's name

The preview shows SuperCalls about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows SuperCalls about to be written

Select the SuperCalls() line and, above it, type the blank line the book opens the program with. Saves when typed.

The EK9 IDE once this is done: Select the SuperCalls() line and, above it, type the blank line the book opens the program with

Select the stub, from ready <- true down to its //AI line, and type SuperCalls's body exactly as the chapter shows it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type SuperCalls's body exactly as the chapter shows it

Step 4: Run it

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

The project's only program is already chosen. Choose Run.

Ada holds 120.00#GBP; Bob holds 800.00#GBP at 3.5% - twice, because asAccount is the same object seen through its parent type, and the override still runs; then the charges 2.50#GBP and 0.00#GBP. Choose Close.

The EK9 IDE just before choosing Close: Ada holds 120.00#GBP; Bob holds 800.00#GBP at 3.5% - twice, because asAccount is the same object seen through its parent type, and the override still runs; then the charges 2.50#GBP and 0.00#GBP

The result

SuperCall.ek9 now reads exactly as chapter 29 of EK9 in Depth shows it. SavingsAccount's constructor hands holder and balance up with super(...) and keeps only the rate for itself; its describe adds to the parent's text, and its monthlyCharge scales the parent's charge by zero.

The EK9 IDE when the lesson has finished

Where this lesson comes from