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.
- Solidity - Types
- Solidity - Functions
- What are Events in Solidity?
- Solidity - Inheritance
- Solidity - Polymorphism
- Solidity - View and Pure Functions
- Solidity - Abstract Contract
- How to Install Solidity in Windows?
- Solidity - While, Do-While, and For Loop
- Solidity - Break and Continue Statements
- Solidity - Variables
- Solidity - Error Handling
- Steps to Execute Solidity Smart Contract using Remix IDE
- Solidity - Arrays
- Solidity - Enums and Structs
- Solidity - Libraries
- Introduction to Solidity
- Solidity - Constructors
- Solidity - Decision Making Statements
- Solidity - Operators
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.