4

Let's assume I have the following two contracts Hello and Goodbye, and Goodbye inherits from Hello. Both declare a constant variable aString, i.e. the child contract Goodbye overwrites the parent's value:

pragma solidity ^0.4.17;


contract Hello {
    // This will be overwritten by child
    string public constant aString = 'Hello World!';

    // Prints Hello World
    function printMe() returns (string){
        return aString;
    }
}


contract Goodbye is Hello{
    // Overwrites `aString` of parent
    string public constant aString = 'Goodbye World!';

    // Why does this print Hello World, too???
    function printMe() returns (string){
        return super.printMe();
    }

    // Prints Goodbye World as expected
    function printMe2() returns (string){
        return aString;
    }
}

So if I call printMe() of contract Hello it returns 'Hello World!' as expected.

Moreover, if I create the contract Goodbye and call its function printMe2() it returns 'Goodbye World!' because aString overwrites the parent's definition of 'Hello World!'. However, if call printMe() of Goodbye which, in turn, calls super.printMe() it returns 'Hello World!'?

Why does printMe() in Goodbye not return 'Goodbye World!'? Why is the overwriting of aString by Goodbye ignored by the super.printMet() call?

1 Answer 1

3

Well you say it yourself, you're not calling aString, but super.printMe().

Have a deeper explanation of superhere:

The super keyword in Solidity gives access to the immediate parent contract from which the current contract is derived.

You are explicitly calling the parent contract Hello, in which aString is 'Hello, world.'

Look at the difference if in printMe2() you put:

return super.aString;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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