41

Solidity 0.6.0 introduced the keywords virtual and override. What do they do?

1 Answer 1

53

As of Solidity 0.6.0, the keywords virtual and override are available natively in Solidity for function overriding. The purpose of these keywords is to be more explicit when overriding a function. Base functions can be overridden by inheriting contracts to change their behavior if they are marked as virtual. The overriding function must then use the override keyword in the function header.

These keywords simply allow for more explicit behavior when overriding functions. Prior to version 0.6.0 of Solidity, these keywords did not exist and function overriding was done implicitly. These keywords allow developers to explicitly override functions. Additionally, they allow developers to signal their intent for certain functions so that others have a better understanding of the purpose of the function.

There are a few things to note when using these keywords:

  • For multiple inheritance, the most derived base contracts that define the same function must be specified explicitly after the override keyword.

  • Functions with the private visibility cannot be virtual.

  • Functions without implementation have to be marked virtual outside of interfaces. In interfaces, all functions are automatically considered virtual.

Examples

Simple Example (From Solidity Docs)

pragma solidity >=0.5.0 <0.7.0;

contract Base {
    function foo() virtual public {}
}

contract Middle is Base {}

contract Inherited is Middle {
    function foo() public override {}
}

Multiple Inheritance Example (From Solidity Docs)

pragma solidity >=0.5.0 <0.7.0;

contract Base1 {
    function foo() virtual public {}
}

contract Base2 {
    function foo() virtual public {}
}

contract Inherited is Base1, Base2 {
    // Derives from multiple bases defining foo(), so we must explicitly
    // override it
    function foo() public override(Base1, Base2) {}
}
5
  • 2
    Is Solidity virtual function the same as Java abstract method? Nov 27, 2021 at 1:14
  • 1
    Can I override a function from the Base contract if it's not marked as virtual?
    – Qwerty
    Dec 30, 2021 at 4:45
  • 1
    @Qwerty if you try to override a base class function without virtual keyword then you ll get the below error .. (Test on Compiler version 0.6.4 and above) from solidity: TypeError: Trying to override non-virtual function. Did you forget to add "virtual"?
    – CaptPython
    Apr 6 at 4:45
  • 1
    @NicolasMassart I believe it not the case becuse ABSTRACT METHOD in Java, is a method that has just the method definition but does not contain implementation. but a virtual function in Contract can have it own implementation as well
    – CaptPython
    Apr 6 at 4:47
  • Does this mean functions that are not virtual cannot be overridden anymore since v0.6.0?
    – jorijnsmit
    Apr 19 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.