CodeBlanks Logo Icon CodeBlanks

Summary - Encapsulation

Combine private, getter/setter and static to write an Account class.

Summary - Encapsulation

In this group you saw the basic tools of encapsulation: private, getter/setter, final/const, late and static. Now we will merge them in an Account class.

An Account class will hold a private _balance field and offer controlled access through a getter and setter. It will also count every new account via a static int totalAccounts counter. Combining the two gives instance level privacy and class level sharing, a complete example of encapsulation.

Practice Add a private field and accessors to Account

Define a class named Account. Add a private double _balance field. Write an Account(double initial) : _balance = initial; constructor. Add a double get balance => _balance; getter and a set balance(double value) { if (value < 0) return; _balance = value; } setter. In main, create an instance with var a = Account(100); and call print(a.balance).

Starter code

main.dart
// Define the Account class
// Add a private _balance, constructor, getter and setter

void main() {
  // Create an Account(100) instance and print a.balance
}

Sample solution

main.dart
// Define the Account class
class Account {
  double _balance;
  Account(double initial) : _balance = initial;
  double get balance => _balance;
  set balance(double value) {
    if (value < 0) return;
    _balance = value;
  }
}

void main() {
  var a = Account(100);
  print(a.balance);
}

Expected output

100.0

Add a static counter to Account

In the second step we add a static counter to the Account class. This counter tracks at class level how many Account instances have been created.

Example: a static int totalAccounts = 0; field and totalAccounts++; inside the constructor. Each new Account instance increments the counter. Read it via Account.totalAccounts on the class; no instance is needed. This shape fits collecting statistics, tracking resources and numbering instances uniquely.

Practice Add a static counter to Account

Keep the existing Account class (private _balance, constructor, getter, setter). Add a static int totalAccounts = 0; field to the class. Add a totalAccounts++; statement to the constructor body (open a constructor body after the _balance = initial initializer list). In main, create two instances with var a = Account(100); and var b = Account(200);, then call print(Account.totalAccounts) and print(a.balance).

Starter code

main.dart
class Account {
  double _balance;
  Account(double initial) : _balance = initial;
  double get balance => _balance;
  set balance(double value) {
    if (value < 0) return;
    _balance = value;
  }
}

// Add a static counter field to the class and increment it in the constructor body

void main() {
  // Create two Account instances and print totalAccounts and a.balance
}

Sample solution

main.dart
class Account {
  double _balance;
  static int totalAccounts = 0;
  Account(double initial) : _balance = initial {
    totalAccounts++;
  }
  double get balance => _balance;
  set balance(double value) {
    if (value < 0) return;
    _balance = value;
  }
}

void main() {
  var a = Account(100);
  var b = Account(200);
  print(Account.totalAccounts);
  print(a.balance);
}

Expected output

2
100.0

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which is true for an Account class?

  • balance must be static
  • _balance is public, no balance method exists
  • _balance is private, balance is reached through getter/setter Correct answer
  • _balance must be marked late

Explanation: A private field plus accessors is the core of encapsulation.

All lessons in Intermediate