When should you put the virtual keyword on a function?
A function that allows an inheriting contract to override its behavior should be marked as ‘virtual’
-
Functions without implementation should be marked ‘virtual’ outside of interfaces
-
All functions in interface contracts are automatically considered virtual (no ‘virtual’ declaration is required)
- e.g., see @openzeppelin IERC20.sol which is the Interface of the ERC20 standard as defined in the EIP
-
Functions with the ‘private’ visibility cannot be ‘virtual’
When should you put the keyword override on a function?
A function that overrides a base function should be marked as ‘override’
-
In the case of multiple inheritance (base contracts not related), all direct base contracts should be specified explicitly
-
In the case of multiple inheritance (on some path through the inheritance graph), the most derived base contracts that define the same function must be specified explicitly after the override keyword.
Why would a function have both virtual and override keywords on it?
A function that is marked as both ‘virtual’ and ‘override’, means the function overrides a base function and also allows an inheriting contract to override its behavior