53

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

Shane Fontaine
  • 18,036
  • 20
  • 54
  • 82

1 Answers1

67

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) {} }

Shane Fontaine
  • 18,036
  • 20
  • 54
  • 82
  • 4
    Is Solidity virtual function the same as Java abstract method? – Nicolas Massart Nov 27 '21 at 01:14
  • 2
    Can I override a function from the Base contract if it's not marked as virtual? – Qwerty Dec 30 '21 at 04:45
  • 2
    @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 06 '22 at 04:45
  • 3
    @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 06 '22 at 04:47
  • 3
    Does this mean functions that are not virtual cannot be overridden anymore since v0.6.0? – gosuto Apr 19 '22 at 08:20
  • 1
    Yes that is the case for >= v0.6.0 – yparesh Dec 12 '22 at 05:07