How to Properly Define an Interface in Solidity 0.6 and Above

interfacessoliditysolidity-0.6.x

One of the breaking changes of solidity 0.5 was making explicit function visibility mandatory, hence all interface functions must be defined as external today.

In solidity 0.6, there seems to be two new function modifiers introduced: virtual and override.

How do they overlap with the previous rules for interface functions?

Best Answer

Answer

There are two new rules to be aware of:

  1. All interface functions are implicitly virtual. If you want to be explicit about it, the compiler throws a warning.
  2. All functions inheriting from the interface must set the override modifier on every function that overrides an interface function, lest the compiler throws an error.

Example

Try to see what happens if you add virtual or remove override from this:

interface MyInterface {

    function getBlockNumber() external view returns (uint256);
}

contract MyContract is MyInterface {

    function getBlockNumber() public view override returns (uint256) {
        return block.number;
    }
}

Further Documentation

Note that the virtual and override modifiers don't apply only to interfaces. I recommend reading more about them in this other thread and the solidity 0.6 breaking changes docs.