[Ethereum] Override parent method and modifiers

inheritancemodifiersoverride

For example, we have a contract:

contract A {

    modifier checkCaller() {
        require(msg.sender == 0x123);
        _;
    }

    function doSmth() checkCaller {}
}

We make child contract and override function:

contract B is A {
    function doSmth() {
        super.doSmth();
        // ... do more
    }
}

Do I need to explicitly use modifier checkCaller in the child contract B? As I understand super.doSmth() will throw properly. Correct?

Best Answer

Yes, you are correct.


Something I noticed which is worth keeping an eye on is that both modifiers of 'parent' contracts as well as any functions are overwritten by inheritance even when using 'super'.

For example, calling test(0) on B passes and returns 10 (not 5 as one may be led to believe), but fails on contract A.

pragma solidity ^0.4.15;

contract A {
    modifier only (uint test) {
        require(test > 5);
        _;
    }
    function test(uint test) public only(test) returns(uint) {
        return addSome(test);
    }
    function addSome(uint number) internal returns(uint) {
        return number + 5;
    }
}

contract B is A {
    modifier only (uint test) {
        require(test < 10);
        _;
    }
    function test(uint test) public only(test) returns(uint) {
        return super.test(test);
    }
    function addSome(uint number) internal returns(uint) {
        return number + 10;
    }
}
Related Topic