Object Oriented Programming in Dart: Encapsulation
What is Encapsulation in Dart? Real-World Example of Encapsulation. What is `_` (Underscore) in Dart? How to write secure and reliable code?

Search for a command to run...
What is Encapsulation in Dart? Real-World Example of Encapsulation. What is `_` (Underscore) in Dart? How to write secure and reliable code?

No comments yet. Be the first to comment.
This series covers all the important concepts of OOPs in Dart i.e: Classes, Objects, Constructor, Abstraction, Inheritance, Polymorphism, and Encapsulation.
What is Inheritance in Dart? What are the types of Inheritance? What are the `extends` and `with` keywords and the differences between them?
Lessons from Code, Content, and Consistency

Reliability basics: Outbox pattern + why it matters

Introducing Kafka/Redpanda + move to event-driven workflow

Connect Orders β Inventory (first working service-to-service flow)

Inventory service + gRPC + proto contracts

extends and with. Then we went through different types of Inheritance.extends, with, and implement keywords.



Now suppose, you made the Variables of the library Public, which means, now anyone with your library imported inside their code, has the ability to Read and Write into that variable.
So, For example, a developer named John created an app that shows Account Balance. As John doesn't have the access to the Bank's DB directly he used the library that you've created to fetch the details.


One thing to understand here is, Protecting your variables has nothing to do with Hackers. It has to do with other Classes only that you use. This is like giving the key to your home to neighbors whom you want to access your home, still, you want your locker room to be locked and not grant access to it. So think of variables as a safe box that you want to be private and not with public access.
The above example is highly inspired by the Amruta Jahagirdar's Answer.
Now, the question that comes into our mind is, How can we achieve this via Coding? Let's see.
class BankDetails {
int? bankBalance;
}
void main (){
final john = BankDetails();
john.bankBalance = 1000;
print(john.bankBalance);
}
Here, john.bankBalance is used to set the value 1000 then john.bankBalance to get the value and then printed it.
Now how can we make the field bankBalance private so no outer class has access to it
public, private, and protected._ (Underscore) at the starting of the variable name (ex: _variableName).// test.dart
class BankDetails {
int? _bankBalance;
}
// main.dart
import 'test.dart'
void main (){
final john = BankDetails();
john.bankBalance = 1000; // Error
print(john.bankBalance); // Error
}
If youβre curious why Dart uses underscores instead of access modifier keywords like a public or private, see SDK issue 33383
// test.dart
class BankDetails {
BankDetails({
required this.password,
});
int? _bankBalance;
String password;
set setBalance(int newBalance) {
if(password == getActualPasswordFromDB())
_bankBalance = newBalance;
}
get getBalance => _bankBalance ?? 'UnAuthorized Access';
}
// main.dart
import 'test.dart';
void main() {
final john = BankDetails(password: '1234');
john.setBalance = 1000;
print(john.getBalance);
}
