Sub topic 1
Classes and objects
Prerequisites
This chapter assumes that you know basic Dart syntax, variables, types, functions, and control flow. You should also understand nullable and non-nullable types and have seen objects that call methods.
A class describes a kind of object
A class is a blueprint for objects. It defines the data an object stores and the operations it can perform. The data is called state. The operations are called behavior.
An object is a concrete instance of a class. Two objects can come from the same class while holding different state.
| Concept | Meaning | Example |
|---|---|---|
| Class | A definition for a kind of object | `UserProfile` |
| Object | One instance created from a class | A profile for Sam |
| Field | Data stored by an object | `name` |
| Method | A function belonging to an object | `addPoints` |
A class can contain fields and methods. A field usually represents a value that can change or describe the object. A method changes state or calculates a result.
Define and use a class
The following class models a user profile:
class UserProfile {
String name = 'Guest';
int points = 0;
String get summary => '$name: $points points';
void addPoints(int amount) {
points += amount;
}
}
void main() {
final firstUser = UserProfile();
firstUser.name = 'Sam';
firstUser.addPoints(10);
final secondUser = UserProfile();
secondUser.name = 'Lee';
secondUser.addPoints(25);
print(firstUser.summary);
print(secondUser.summary);
}UserProfile is the class. firstUser and secondUser are separate objects. Each object has its own name and points values. Changing firstUser does not change secondUser.
The expression UserProfile() creates an object. In this example, Dart supplies a simple no-argument constructor automatically. Constructors are covered in detail later.
The summary member is a getter. A getter looks like a field when it is read, but Dart calculates its value by running code. The addPoints member is a method because it performs an action.
A variable declared with final cannot refer to a different object later, but the object itself may still be mutable. Therefore, firstUser cannot be replaced with another profile, while firstUser.points can change.
Think in responsibilities
A useful class has a focused responsibility. UserProfile owns profile data and knows how to add points. Code outside the class asks the object to perform an operation instead of duplicating the operation's rules.
For example, calling firstUser.addPoints(10) keeps point updates in one place. If the rules later require validation, you can change the method without changing every caller.
Summary
- A class defines the structure and behavior of a type.
- An object is an instance of that class.
- Fields store state, methods provide behavior, and getters calculate readable values.
- Each object has independent instance state.
- A focused class should protect the rules for the data it owns.
Exercise
After final profile = UserProfile(); profile.addPoints(5);, which value changes: the class definition, the object referenced by profile, or every UserProfile object?
Show answer
The object referenced by profile changes. Other UserProfile objects keep their own points values, and the class definition does not change.