-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path26_VisibilitySpecifiers.sol
54 lines (37 loc) · 1.54 KB
/
26_VisibilitySpecifiers.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract VisibilitySpecifiers{
// State variables can be declared as public, private, or internal but not external.
uint private x; // You cannot read the value outside the contract
uint internal y;
uint public z; // You can read the value outside the contract
function privateFunction() private {
x = 123;
}
// We put an underscore for internal functions as a part of convention
function _internalFunction() internal{
y = 456;
}
// This function can be accessed outside the contract
// visible externally and internally
function publicFunction() public {
z = 789;
}
// This function can be accessed outside the contract
// You cannnot call this function inside the same contract.
function externalFunction() external view returns (address){
return msg.sender;
}
}
contract TestContract is VisibilitySpecifiers{
function testTheVisibility() external returns (uint){
// uint derivedValue = x; 👈 This will give an error as we cannot access private variables
uint derivedValue = y; // We can inherit the internal variable ✔
uint derivedValue1 = z; // We can inherit the public variable ✔
// privateFunction(); 👈 This will give an error as we cannot access private functions.
_internalFunction(); // We can access the internal function ✔
publicFunction(); // We can access the public function ✔
// externalFunction(); We cannot access the external function ❌
return (derivedValue+derivedValue1);
}
}