Virtual & Overwrite
In Solidity, the virtual
and override
keywords are used when working with inheritance and function overriding.
virtual
keyword is used to declare a function that can be overridden in the child contract. When a function is declared asvirtual
, it means that it can be overridden by a function with the same signature in a child contract.override
keyword is used to indicate that a function is intended to override a virtual function in the parent contract. When a function is declared with theoverride
keyword, it must have the exact same function signature as the function it is intended to override.
Example
In this example, the Parent
contract declares a virtual function foo()
. The Child
contract inherits from the Parent
contract and overrides the foo()
function with its own implementation using the override
keyword.
Last updated