Solidity – Encapsulation
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. 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. 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.