typestar

Abstract contracts in Solidity

Leave a hole a child must fill.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

// An abstract contract leaves a hole a child must fill.
abstract contract Stand {
    uint256 public sold;

    function price() public pure virtual returns (uint256);

    function sell(uint256 n) external {
        sold += n * price();
    }
}

contract AppleStand is Stand {
    function price() public pure override returns (uint256) {
        return 3;
    }
}

How it works

  1. abstract contract Stand cannot deploy alone.
  2. price() is declared virtual with no body.
  3. AppleStand overrides it and becomes deployable.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
384
Tokens
67
Three-star pace
60 tpm

At the three-star pace of 60 tokens a minute, this run takes about 67 seconds.

Type this snippet

Step 3 of 4 in Composition, step 19 of 28 in Language basics.

← Previous Next →