Encapsulation is fundamental and one of the most important concepts of object-oriented programming languages. It refers to the mechanism of manipulation of the scope of variables, i.e. it restricts the access of the variable outside the scope. It allows enough constraint access to a method for taking action on it. The scope of the objects can have four types of access:
- Public: These objects can be accessed internally and externally as well as via messages also. Public elements can be inherited by external methods. A getter function is generated automatically when a public variable is created.
- Internal: As the name suggests, Internal objects can be accessed by internal methods or by derived methods but cannot be accessed by external ones. Only a base contract and a derived contract has its access.
- Private: Private objects can only be accessed internally from the current contract instances. They cannot be accessed by derived methods also.
- External: These objects can be accessed externally but not internally i.e. current contract instances cannot access it. They cannot be inherited.
Example: In the below example, the contract parent is inherited by the contract child to demonstrate different scopes of the object discussed above.
Solidity
pragma solidity >=0.4.22 <0.9.0;
contract parent {
uint public num = 30;
uint internal internal_num= 10;
function sqrt () external returns (
uint) {
internal_num = internal_num ** 2;
return internal_num;
}
}
contract Caller {
child c = new child();
function f() public returns (
uint) {
return c. sqrt ();
}
function f2() public returns(
uint, uint){
return (c.increment(), c.add());
}
}
contract child is parent {
function increment(
) public payable returns (uint) {
num = num + 20;
return num;
}
function add() public pure returns(
uint){
uint a = 10;
uint b = 20;
uint sum = a + b;
return sum;
}
}
|
Output: Deploy the Caller Contract to get the output of the given Example.
1. Output of f():
{
"0": "uint256: 100"
}
2. Output of f2() :
{
"0": "uint256: 50",
"1": "uint256: 30"
}
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...