Encapsulation in Dart
Hello everyone!

Today I am here to share the concept of encapsulation in Object Oriented Programming (OOP) in dart. There are four pillars of OOP.
- Encapsulation
- Inheritance
- Abstraction
- Polymorphism
Encapsulation
Encapsulation means hiding data in object oriented programming. This is an important concept and widely used in the programming. Sometimes we need to hide specific data in an application to deceive the hackers. So for this purpose we use encapsulation.
In the encapsulation we declare private variables in the class and those are only accessible within that class. They cannot be directly accessed in other classes or files and in libraries in dart. So in order to access them we use special methods which are the getter and setter methods.
- Getter method is used to get the value of the private variables.
- Setter methods help to set new value to the private variables.
Dart is different than other languages. In other languages we can define private variables within single file and then use them using getter and setter methods but in dart programming language we have to create a separate file for the class where we want to declare private variables.
Here I will show a simple and basic example with you by using the private variable, getter, and setter method.

I have written a Person class and in this class age is a private integer.
Then I have defined the setter method because after setting the value we will be able to get the value. The setter method is very easy. In the start we have to use set keyword and then name of the setter method (any name) then as a parameter we have to pass the data type and the name of the variable. In the body I have used a condition to check if the age is valid or not. I am checking if the age is valid then I am setting that new age in the private variable and updating it with new value.
At the end after setting the value I have used a getter method. The syntax of the getter method is also very simple. First of all we have to write the datatype of the variable according to the type of the private variable. Then we have to use get keyword and using arrow operation the getter is made. Here the getter of the _age is age. Now we will be able to access this private variable through this getter variable.

This is the main file where I have imported the person.dart file. I have created the object of the class and then using the object I have set the value of the age using the setter method. Now the age has a value and then I have used getter variable age to display the age.

This is how encapsulation works in programming. We hide data using private variables and then we can access them using the getter method and we can set their values using the setter method.
🎉 Congratulations!
Your post has been manually upvoted by the SteemX Team! 🚀
SteemX is a modern, user-friendly and powerful platform built for the Steem ecosystem.
🔗 Visit us: www.steemx.org
✅ Support our work — Vote for our witness: bountyking5
Visit: https://steemhop.org
🎉 Congratulations, @mohammadfaisal!
Your post Encapsulation in Dart was rewarded by CCS Curation Trail
“Home is where your heart is ❤️.”
👉 Join the CCS Curation Trail | Community Invitation
✅ Vote for visionaer3003 as witness