0
0
Blockchain / Solidityprogramming~10 mins

Visibility modifiers (public, private, internal, external) in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Visibility modifiers (public, private, internal, external)
Define Function with Visibility
Call Function
Check Visibility Modifier
public
Accessible
Functions have visibility rules that control where they can be called from: public (anywhere), private (only inside the contract), internal (inside and derived contracts), external (only from outside).
Execution Sample
Blockchain / Solidity
contract Example {
  function pub() public {}
  function priv() private {}
  function inter() internal {}
  function ext() external {}
}
Defines four functions each with a different visibility modifier to control access.
Execution Table
StepFunction CalledCaller LocationVisibility CheckAllowed?Reason
1pub()Inside contractpublic allows allYesPublic functions can be called anywhere
2priv()Inside contractprivate only insideYesPrivate functions accessible inside contract
3priv()Derived contractprivate only insideNoPrivate not accessible in derived contracts
4inter()Derived contractinternal inside+derivedYesInternal accessible in derived contracts
5inter()Outside contractinternal inside+derivedNoInternal not accessible externally
6ext()Outside contractexternal only externalYesExternal functions callable from outside
7ext()Inside contractexternal only externalNoExternal functions not callable internally
8pub()Outside contractpublic allows allYesPublic functions callable anywhere
💡 Execution stops after all visibility cases are checked.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7Final
Allowed?N/AYesYesNoYesNoYesNoYes
Key Moments - 3 Insights
Why can't private functions be called from derived contracts?
Private functions are only accessible inside the contract where they are defined, as shown in step 3 of the execution_table where the call from a derived contract is denied.
Why are external functions not callable from inside the contract?
External functions require calls from outside the contract, so internal calls fail as in step 7 of the execution_table.
What makes internal functions different from private?
Internal functions can be called inside the contract and from derived contracts, unlike private which is only inside the contract, as seen in steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is a private function call denied?
AStep 2
BStep 3
CStep 5
DStep 7
💡 Hint
Check the 'Allowed?' column for 'No' and the 'Function Called' column for 'priv()'
According to variable_tracker, what is the Allowed? value after step 6?
ANo
BN/A
CYes
DUnknown
💡 Hint
Look at the 'After 6' column in variable_tracker row 'Allowed?'
If the function ext() was called from inside the contract, what would happen according to execution_table?
ACall denied
BCall allowed
CCall allowed only if public
DCall allowed only if internal
💡 Hint
See step 7 in execution_table where ext() is called inside contract and is denied
Concept Snapshot
Visibility modifiers control who can call functions:
- public: callable anywhere
- private: only inside the contract
- internal: inside and derived contracts
- external: only from outside calls
Use them to protect your contract's functions.
Full Transcript
This visual execution shows how visibility modifiers in blockchain smart contracts control function access. Public functions can be called anywhere, private only inside the contract, internal inside and derived contracts, and external only from outside. The execution table traces calls to each function from different locations and checks if they are allowed. The variable tracker shows the allowed status after each step. Key moments clarify common confusions about private and external functions. The quiz tests understanding by referencing the execution steps and variable states.