🎓 A1Learner
🔍
0 of 48 lessons completed

Solidity Override Rules

Override rules say how a child replaces a parent function. A child must mark the replacement and name every parent that defines the function.

A replacement without override fails

The child defines value again but leaves out override. The compiler rejects it.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract A {
    function value() public pure virtual returns (uint256) {
        return 1;
    }
}

contract B is A {
    function value() public pure returns (uint256) {
        return 2;
    }
}

An override list missing a parent fails

Both B and C define value. The child names only B, so the compiler rejects it.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract A {
    function value() public pure virtual returns (uint256) {
        return 1;
    }
}

contract B is A {
    function value() public pure override virtual returns (uint256) {
        return 2;
    }
}

contract C is A {
    function value() public pure override virtual returns (uint256) {
        return 3;
    }
}

contract D is B, C {
    function value() public pure override(B) returns (uint256) {
        return 5;
    }
}

Naming every parent compiles

The child lists B and C. Its own version wins and returns 5.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract A {
    function value() public pure virtual returns (uint256) {
        return 1;
    }
}

contract B is A {
    function value() public pure override virtual returns (uint256) {
        return 2;
    }
}

contract C is A {
    function value() public pure override virtual returns (uint256) {
        return 3;
    }
}

contract D is B, C {
    function value() public pure override(B, C) returns (uint256) {
        return 5;
    }
}

Example

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

contract BaseA {
    function score() public pure virtual returns (uint256) {
        return 10;
    }
}

contract BaseB {
    function score() public pure virtual returns (uint256) {
        return 20;
    }
}

contract Combined is BaseA, BaseB {
    function score() public pure override(BaseA, BaseB) returns (uint256) {
        return 30;
    }
}

Key points

  • Virtual first: A parent function must be virtual before any child can replace it.
  • Override keyword: A replacing function must carry the override keyword.
  • Every parent: When several parents define the function, the child lists them all, as in override(A, B).
  • Same shape: The replacement keeps the parent function's name and inputs.

In this example, BaseA and BaseB each define score as virtual. Combined inherits from both and overrides score once. Its override list names both parents, and its own value of 30 is the one callers get.

Note

Forgetting a parent in the override list is a compile error, not a runtime surprise.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Remix IDE

Exercise

A child overrides a function defined in two parents. What must its override list contain?