Encapsulation in Dart

in CCS10 hours ago (edited)

Hello everyone!

Today I am here to share another practical example of encapsulation in Object Oriented Programming (OOP) in dart.

I will show you with a simple example but a real world case. We will make a class Employee and then we will calculate the incremented salary on the basis of the basic salary. We will set the condition that the percent of increment must be between 1 to 50.

Here we will use a _salary as a private variable. Then we will create a getter to get the value of salary. WE will define a method which will calculate the increment in the salary and then after increment salary.


image.png

Here you can see I have created class Employee where I will hide the data. I have created a private variable salary. Then I have created the constructor to pass the salary but I have implemented the condition that if the salary is greater than 0 then the salary should be updated otherwise it must be considered zero because we know the salary cannot be negative or zero.

Then I have defined the method which will calculate the salary after the increment. It gets a double percent value and then validate it if the entered percent is between 1 - 50 then it proceeds next otherwise it throws an exception that the percent must be positive and between 1 - 50.

At the end I have created the getter to get the value of the salary.


image.png

In the main I have created the object of the class and passed the value of the salary. Then I have called the method to calculate the salary after increment. At the end I have printed the total salary after increment.

Posted using SteemX

Sort:  

🎉 Congratulations!

Your post has been upvoted by the SteemX Team! 🚀

SteemX is a modern, user-friendly and powerful platform built for the Steem community.

🔗 Visit us: www.steemx.org

✅ Support our work — Vote for our witness: bountyking5

banner.jpg

 9 hours ago 

🎉 Congratulations, @mohammadfaisal!

Your post Encapsulation in Dart was rewarded by CCS Curation Trail

CCS Logo

“Home is where your heart is ❤️.”

👉 Join the CCS Curation Trail | Community Invitation

Vote Banner

Vote for visionaer3003 as witness