Solidity tutorial – Access Modifier

Solidity tutorial - Access Modifier

Access level for State variables

Access
Modifiers
Description
publicCan use public state variables directly from within the contract and access them through the related getter function from external contract or client code.
internalThe contract and any inherited contract can access Internal state variables. This is the default level for state variables.
privateOnly members of the same contract—not inherited contracts—can access private state variables.

Access level for Functions

Access
Modifiers
Description
externalAn external function is exposed in the contract interface, and you can only call it from external contracts or client code but not from within the contract.
publicA public function is exposed in the contract interface and you can call it from within the contract or from external contracts or client code. This is the default accessibility level for functions.
internalAn internal function isn’t part of the contract interface, and it’s only visible to contract members and inherited contracts
private A private function can only be called by members of the contract where it’s been declared, not by inherited contracts.

Example:

contract BiasTekCoin {
         function transfer(address _to, uint256 _amount) public {} //accessible internally and externally
         function checkLimit(uint256 _amount) private returns (bool) {} //only accessible from within this contract
         function validateAccount(address avcount) internal returns (bool) {} //accessible from this and inherited contracts
         function freezeAccount(address target, bool freeze) external {} //only accessible externally
}

Leave a Reply

Your email address will not be published. Required fields are marked *