Visibility modifiers control who can use or see parts of your code. They help keep your program safe and organized.
Visibility modifiers (public, private, internal, external) in Blockchain / Solidity
function functionName() public { }
function functionName() private { }
function functionName() internal { }
function functionName() external { }public: Anyone can call this function.
private: Only the current contract can call this function.
internal: Current contract and contracts that inherit it can call this function.
external: Only outside callers can call this function, not internal calls.
function greet() public {
// Anyone can call this
}function secret() private {
// Only this contract can call this
}function helper() internal {
// This and child contracts can call this
}function externalCall() external {
// Only external calls allowed
}This example shows how different visibility modifiers control access to variables and functions. Private items are only for the contract itself. Internal items can be used by child contracts. Public items are open to everyone.
pragma solidity ^0.8.0; contract VisibilityExample { string private secretMessage = "Hello Private!"; string internal internalMessage = "Hello Internal!"; string public publicMessage = "Hello Public!"; function getSecret() private view returns (string memory) { return secretMessage; } function getInternal() internal view returns (string memory) { return internalMessage; } function getPublic() public view returns (string memory) { return publicMessage; } function callInternal() public view returns (string memory) { return getInternal(); } function tryGetSecret() public view returns (string memory) { // Cannot call getSecret() here because it is private // return getSecret(); // This would cause an error return "Cannot access private function from here"; } } contract ChildContract is VisibilityExample { function childAccess() public view returns (string memory) { // Can access internal but not private return internalMessage; } }
Private functions and variables are not visible outside the contract, even to child contracts.
Internal functions and variables can be accessed by the contract and any contract that inherits it.
External functions can only be called from outside the contract, not from inside.
Public means anyone can use it.
Private means only this contract can use it.
Internal means this contract and child contracts can use it.
External means only outside calls can use it.
Practice
Solution
Step 1: Understand visibility modifiers
Public functions can be accessed anywhere, inside or outside the contract.Step 2: Compare with other modifiers
Private is only inside the contract, internal is inside and inherited contracts, external only callable from outside.Final Answer:
public -> Option AQuick Check:
public = open access [OK]
- Confusing private with public access
- Thinking internal allows outside calls
- Assuming external works inside contract
Solution
Step 1: Identify external function syntax
External functions are declared with the keywordexternalafter the function name.Step 2: Check other options
Private and internal restrict access inside the contract or inheritance, public allows all calls.Final Answer:
function foo() external {} -> Option BQuick Check:
external = outside calls only [OK]
- Using private instead of external
- Confusing public with external
- Missing parentheses or braces
contract Test {
uint private data = 10;
function getData() public view returns (uint) {
return data;
}
function setData(uint _data) external {
data = _data;
}
}What will happen if another contract tries to call
data directly?Solution
Step 1: Check variable visibility
The variabledatais declaredprivate, so it cannot be accessed outside the contract.Step 2: Understand access from other contracts
Other contracts cannot access private variables directly, only public or external functions.Final Answer:
It will fail because data is private -> Option CQuick Check:
private = no outside access [OK]
- Assuming private variables are public by default
- Confusing external with private
- Thinking public functions expose private variables directly
contract Sample {
uint internal count = 0;
function increment() private {
count += 1;
}
function callIncrement() public {
increment();
}
}Which of the following fixes the error preventing
increment() from being called?Solution
Step 1: Check function call context
The functionincrement()is private and called inside the same contract bycallIncrement().Step 2: Understand private function access
Private functions can be called inside the same contract, so this call is valid.Final Answer:
No fix needed, code is correct -> Option AQuick Check:
private functions callable inside contract [OK]
- Thinking private functions can't be called inside contract
- Confusing internal with private
- Changing visibility unnecessarily
Solution
Step 1: Understand access needs
The function should be callable inside this contract and by derived contracts, but not externally.Step 2: Match visibility modifier
Internal visibility allows access inside the contract and by inheriting contracts only.Final Answer:
internal -> Option DQuick Check:
internal = contract + inheritance only [OK]
- Choosing public which allows external calls
- Choosing private which blocks inheritance access
- Confusing external with internal
Ready to prove it?
Pass the test and this pattern moves unknown
