📖 Scenario: You are creating a simple smart contract for a blockchain application. You want to control who can access certain functions and variables in your contract using visibility modifiers.
🎯 Goal: Build a Solidity contract that uses public, private, internal, and external visibility modifiers correctly to control access to functions and variables.
📋 What You'll Learn
Create a contract named VisibilityDemo
Declare a public state variable publicData of type uint with value 10
Declare a private state variable privateData of type uint with value 20
Declare an internal state variable internalData of type uint with value 30
Create a public function getPrivateData that returns the value of privateData
Create an external function externalFunction that returns the sum of publicData and internalData
💡 Why This Matters
🌍 Real World
Visibility modifiers help protect sensitive data and control who can use certain functions in blockchain smart contracts, preventing unauthorized access.
💼 Career
Understanding visibility is essential for blockchain developers to write secure and efficient smart contracts that behave as intended.
Progress0 / 4 steps
1
Create the contract and public variable
Create a Solidity contract named VisibilityDemo and declare a public state variable called publicData of type uint with the value 10.
Blockchain / Solidity
Hint
Use contract VisibilityDemo { } to start your contract and declare uint public publicData = 10; inside it.
2
Add private and internal variables
Inside the VisibilityDemo contract, declare a private state variable called privateData of type uint with value 20, and an internal state variable called internalData of type uint with value 30.
Blockchain / Solidity
Hint
Use uint private privateData = 20; and uint internal internalData = 30; inside the contract.
3
Add public and external functions
Inside the VisibilityDemo contract, create a public function called getPrivateData that returns the value of privateData. Also, create an external function called externalFunction that returns the sum of publicData and internalData.
Blockchain / Solidity
Hint
Define function getPrivateData() public view returns (uint) that returns privateData. Define function externalFunction() external view returns (uint) that returns publicData + internalData.
4
Test the contract output
Write a Solidity function called testOutput inside the VisibilityDemo contract that calls getPrivateData and externalFunction and returns their results as a tuple of two uint values.
Blockchain / Solidity
Hint
Use this.externalFunction() to call the external function from inside the contract. Return both values as a tuple.
Practice
(1/5)
1. Which visibility modifier allows a function to be called both inside and outside the contract, including by other contracts and users?
easy
A. public
B. private
C. internal
D. external
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 A
Quick Check:
public = open access [OK]
Hint: Public means open to all inside and outside calls [OK]
Common Mistakes:
Confusing private with public access
Thinking internal allows outside calls
Assuming external works inside contract
2. Which of the following is the correct way to declare a function that can only be called from outside the contract in Solidity?
easy
A. function foo() private {}
B. function foo() external {}
C. function foo() internal {}
D. function foo() public {}
Solution
Step 1: Identify external function syntax
External functions are declared with the keyword external after 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 B
Quick Check:
external = outside calls only [OK]
Hint: External functions use keyword 'external' after function name [OK]
Common Mistakes:
Using private instead of external
Confusing public with external
Missing parentheses or braces
3. Consider the following Solidity contract snippet:
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?
medium
A. It will succeed and return 10
B. It will fail because data is external
C. It will fail because data is private
D. It will succeed because data is public by default
Solution
Step 1: Check variable visibility
The variable data is declared private, 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 C
Quick Check:
private = no outside access [OK]
Hint: Private variables can't be accessed outside their contract [OK]
Common Mistakes:
Assuming private variables are public by default
Confusing external with private
Thinking public functions expose private variables directly
4. Given this Solidity code snippet:
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?
medium
A. No fix needed, code is correct
B. Change increment() visibility to internal
C. Change increment() visibility to external
D. Change increment() visibility to public
Solution
Step 1: Check function call context
The function increment() is private and called inside the same contract by callIncrement().
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 A
Quick Check:
private functions callable inside contract [OK]
Hint: Private functions work inside same contract only [OK]
Common Mistakes:
Thinking private functions can't be called inside contract
Confusing internal with private
Changing visibility unnecessarily
5. You want to create a Solidity contract where a function can only be called by this contract or contracts that inherit from it, but not by external users. Which visibility modifier should you use for this function?
hard
A. public
B. external
C. private
D. internal
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 D
Quick Check:
internal = contract + inheritance only [OK]
Hint: Internal means accessible inside and by child contracts only [OK]
Common Mistakes:
Choosing public which allows external calls
Choosing private which blocks inheritance access
Confusing external with internal
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.