Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Encapsulation, Getters and Setters | Classes and Objects
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Dart OOP Essentials

bookEncapsulation, Getters and Setters

Note
Definition

Encapsulation is the object-oriented programming principle of hiding internal data and only allowing access through controlled interfaces, such as getters and setters. In the BankAccount and Temperature examples, private fields and controlled access methods ensure that data cannot be changed in unsafe or unintended ways.

Encapsulation means restricting direct access to an object's data and exposing it only through controlled methods. In Dart, you do this by using private fields, getters, and setters. This keeps your objects safe, consistent, and easier to maintain.

Getter
expand arrow

A getter is a special method that lets you read a private field's value from outside the class, without giving direct access. You define a getter using the get keyword. For instance, double get balance => _balance; lets you access the balance safely.

Setter
expand arrow

A setter is a special method that lets you update a private field's value from outside the class, but with control. You define a setter using the set keyword. For example, you can write set deposit(double amount) { if (amount > 0) { _balance += amount; } } to ensure only positive amounts are added.

bank_account.dart

bank_account.dart

copy
12345678910111213141516171819
class BankAccount { double _balance = 0; double get balance => _balance; set deposit(double amount) { if (amount > 0) { _balance += amount; } } } void main() { var account = BankAccount(); account.deposit = 100; print("Current balance: \$${account.balance}"); account.deposit = -50; // Invalid deposit, ignored print("After invalid deposit: \$${account.balance}"); }

The BankAccount class above shows how encapsulation works in practice. The _balance field is private, so you cannot change it directly from outside the class. Instead, you use the deposit setter, which only allows positive amounts to be added. The balance getter lets you read the value safely. This ensures that the balance cannot become negative or be changed in unexpected ways, protecting the integrity of your data.

temperature.dart

temperature.dart

copy
12345678910111213141516171819
class Temperature { double _celsius = 0; double get celsius => _celsius; set celsius(double value) { if (value >= 0) { _celsius = value; } } } void main() { var temp = Temperature(); temp.celsius = 25; print("Temperature: ${temp.celsius}"); temp.celsius = -10; // Invalid, ignored print("After invalid set: ${temp.celsius}"); }
question mark

Why is it recommended to use getters and setters instead of making fields public in Dart?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 3

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you give another example of encapsulation in Dart?

What are the benefits of using getters and setters in Dart?

How does encapsulation improve code safety and maintenance?

bookEncapsulation, Getters and Setters

Glissez pour afficher le menu

Note
Definition

Encapsulation is the object-oriented programming principle of hiding internal data and only allowing access through controlled interfaces, such as getters and setters. In the BankAccount and Temperature examples, private fields and controlled access methods ensure that data cannot be changed in unsafe or unintended ways.

Encapsulation means restricting direct access to an object's data and exposing it only through controlled methods. In Dart, you do this by using private fields, getters, and setters. This keeps your objects safe, consistent, and easier to maintain.

Getter
expand arrow

A getter is a special method that lets you read a private field's value from outside the class, without giving direct access. You define a getter using the get keyword. For instance, double get balance => _balance; lets you access the balance safely.

Setter
expand arrow

A setter is a special method that lets you update a private field's value from outside the class, but with control. You define a setter using the set keyword. For example, you can write set deposit(double amount) { if (amount > 0) { _balance += amount; } } to ensure only positive amounts are added.

bank_account.dart

bank_account.dart

copy
12345678910111213141516171819
class BankAccount { double _balance = 0; double get balance => _balance; set deposit(double amount) { if (amount > 0) { _balance += amount; } } } void main() { var account = BankAccount(); account.deposit = 100; print("Current balance: \$${account.balance}"); account.deposit = -50; // Invalid deposit, ignored print("After invalid deposit: \$${account.balance}"); }

The BankAccount class above shows how encapsulation works in practice. The _balance field is private, so you cannot change it directly from outside the class. Instead, you use the deposit setter, which only allows positive amounts to be added. The balance getter lets you read the value safely. This ensures that the balance cannot become negative or be changed in unexpected ways, protecting the integrity of your data.

temperature.dart

temperature.dart

copy
12345678910111213141516171819
class Temperature { double _celsius = 0; double get celsius => _celsius; set celsius(double value) { if (value >= 0) { _celsius = value; } } } void main() { var temp = Temperature(); temp.celsius = 25; print("Temperature: ${temp.celsius}"); temp.celsius = -10; // Invalid, ignored print("After invalid set: ${temp.celsius}"); }
question mark

Why is it recommended to use getters and setters instead of making fields public in Dart?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 3
some-alt