Solidity Tutorial | Visibility Modifiers - Public, External, Internal Private

EdRoh
17 Nov 202111:26

Summary

TLDRIn this video, the speaker explains Solidity visibility modifiers—`public`, `private`, `internal`, and `external`—and their significance in controlling access to data and functions within Ethereum smart contracts. Through a hands-on demonstration in Remix IDE, the speaker shows how each modifier restricts or allows visibility for functions both internally (within the contract and derived contracts) and externally (from outside the contract). The video emphasizes the importance of restricting access to sensitive functions for security, with the core principle being to provide the least amount of access necessary to ensure the contract's integrity.

Takeaways

  • 😀 Solidity is a programming language for the Ethereum blockchain used to build smart contracts.
  • 😀 Visibility modifiers in Solidity control how and by whom contract functions and variables can be accessed.
  • 😀 There are four types of visibility modifiers: private, internal, external, and public, ordered by decreasing privacy.
  • 😀 Private functions and variables can only be accessed within the current contract.
  • 😀 Internal functions and variables can be accessed within the current contract and contracts derived from it (inherited).
  • 😀 External functions and variables can be accessed by external contracts or accounts, but not internally.
  • 😀 Public functions and variables can be accessed by both internal and external accounts.
  • 😀 The general rule is to give the minimum level of access necessary for security purposes.
  • 😀 Despite being marked as private, contract data is still visible externally on the blockchain.
  • 😀 The difference between internal and private is that internal functions can be inherited, whereas private ones cannot.
  • 😀 Limiting access to data and functions using visibility modifiers is crucial to ensuring the security of blockchain-based applications.

Q & A

  • What is the purpose of visibility modifiers in Solidity?

    -Visibility modifiers in Solidity are used to control the accessibility of functions and variables in a smart contract. They determine who can access or modify these functions or variables, thereby enhancing security and preventing unauthorized access.

  • What are the four visibility modifiers in Solidity, and in what order of privacy are they listed?

    -The four visibility modifiers in Solidity are: 'private' (highest privacy), 'internal', 'external', and 'public' (least private). These modifiers define who can access the function or variable, either from within the contract or externally.

  • How does the 'private' modifier work in Solidity?

    -'Private' means that a function or variable can only be accessed from the current contract. Other contracts or external accounts cannot access or modify it, ensuring the highest level of privacy.

  • What does the 'internal' modifier do in Solidity?

    -'Internal' allows functions or variables to be accessed within the current contract and from any contracts that inherit from it (derived contracts). It restricts access from external accounts or contracts.

  • What is the difference between 'external' and 'public' modifiers in Solidity?

    -'External' allows access to the function or variable from contracts and accounts outside the current contract, but not internally. In contrast, 'public' allows both internal and external access to the function or variable.

  • Why is it important to limit access to functions and variables in a smart contract?

    -Limiting access is crucial for security. Restricting access prevents unauthorized accounts or contracts from manipulating sensitive data or functions, thus protecting the integrity of the contract and the blockchain environment.

  • What happens when you try to access an 'external' function from within the contract?

    -Attempting to access an 'external' function from within the same contract results in an error. Solidity does not allow internal access to functions marked as 'external'.

  • Can derived contracts access 'private' functions or variables from the parent contract?

    -No, 'private' functions or variables cannot be accessed by derived contracts. Only the parent contract itself can access them, making them inaccessible to contracts that inherit from it.

  • What is the effect of using 'internal' on function inheritance in Solidity?

    -'Internal' functions or variables can be accessed within the current contract as well as in contracts that inherit from it. This allows derived contracts to use and modify these functions or variables.

  • How does the 'public' modifier work in terms of access?

    -'Public' allows both internal and external access to a function or variable. This means that anyone, both within and outside the contract, can access and interact with these functions or variables.

Outlines

plate

此内容仅限付费用户访问。 请升级后访问。

立即升级

Mindmap

plate

此内容仅限付费用户访问。 请升级后访问。

立即升级

Keywords

plate

此内容仅限付费用户访问。 请升级后访问。

立即升级

Highlights

plate

此内容仅限付费用户访问。 请升级后访问。

立即升级

Transcripts

plate

此内容仅限付费用户访问。 请升级后访问。

立即升级
Rate This

5.0 / 5 (0 votes)

相关标签
SolidityVisibility ModifiersEthereumBlockchainSmart ContractsProgrammingCode TutorialInternal FunctionsExternal FunctionsAccess ControlSecurity
您是否需要英文摘要?