0
0
Blockchain / Solidityprogramming~30 mins

Visibility modifiers (public, private, internal, external) in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Visibility Modifiers in Solidity
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use this.externalFunction() to call the external function from inside the contract. Return both values as a tuple.