38

Does the internal keyword in a function definition such as:

function doSomeThing(uint _param) internal {...}

work similar to designating a method as private in other languages and thus makes the function only callable from within the contract?

3 Answers 3

28

The internal modifier can be better compared with protected in object-oriented programming languages. Internal functions of the contract C are visible to the code running at the current address (i.e. the current contract instance) but also to contracts derived from C.

Note that due to the architecture of the EVM, visibility is something that can be strictly enforced at the machine level: There is no way to call a function at another address unless it is explicitly available (i.e. you cannot just "cast" the contract to another type). It is also impossible to call internal or private functions of the same contract type at a different address.

2
  • Well...there is no direct way to do those last things. But an elaborate deposit-challenge-verify mechanism could technically let you circumvent this restriction within another contract. After all, the function's publicly visible in the blockchain. Commented Jan 26, 2016 at 8:30
  • 1
    Yes, there are mechanisms to execute another contract's function (with EXTCODECOPY you do not even need an external service), but not in the context of the other contract.
    – chriseth
    Commented Jan 27, 2016 at 17:20
36

Yes. The internal modifer means that the function can only be called within the contract itself and any derived contracts.

private functions are not available in derived contracts.

From the docs:

internal:

Those functions and state variables can only be accessed internally (i.e. from within the current contract or contracts deriving from it), without using this.

private:

Private functions and state variables are only visible for the contract they are defined in and not in derived contracts.

1

private functions Private functions and state variables are only visible for the contract they are defined in and not in derived contracts.

internal works like protected in object-oriented programming languages with a little difference, you cannot use internal function in the current contract, you must change to external or public. Otherwise, this message is gonna come "TypeError: Overriding function visibility differs."

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.