Ethereum-Solidity Quiz Q17: What visibility modifiers does Solidity use?
Source: Dev.to
Visibility Modifiers Overview
| Modifier | Within Contract | Derived Contract | Other Contract | External |
|---|---|---|---|---|
| public | ✅ | ✅ | ✅ | ✅ |
| internal | ✅ | ✅ | ❌ | ❌ |
| private | ✅ | ❌ | ❌ | ❌ |
| external | ❌ (via this) | ❌ | ✅ | ✅ |
Solidity uses four visibility modifiers to control how functions and state variables can be accessed:
- public – can be called from within the contract, other contracts, and externally.
- external – can be called from other contracts and externally; not from within the same contract (except via
this). - internal – can be called from within the same contract and derived contracts (inheritance); not from other contracts or externally.
- private – can be called only from within the same contract; not from derived contracts, other contracts, or externally.
Important notes
- State variables can be public, internal, or private (they cannot be
external). - Functions can be public, internal, private, or external.
publicstate variables automatically generate getter functions.externalfunctions are more gas‑efficient thanpublicbecause they don’t create an internal function copy.privateandinternaldo not provide security; blockchain data is always visible, they only restrict access via function calls.- Use
externalfor functions intended to be called from outside the contract. - Use
internalfor helper functions used through inheritance. - Use
privatewith caution—typically only for internal helper functions. - Use
publicfor state variables that need to be publicly accessible. - Remember that
privateprovides no security; it merely prevents accidental calls.