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
abstract contract Standcannot deploy alone.price()is declaredvirtualwith no body.AppleStandoverrides it and becomes deployable.
Keywords and builtins used here
contractexternalfunctionispublicreturnreturnsuint256
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.
Step 3 of 4 in Composition, step 19 of 28 in Language basics.