Lesson 22 min

Classes & Objects — Properties & Methods

00:00 / 00:00

Classes and Objects: Properties and Methods

In this lesson, we will implement classes, define properties and methods, and create objects in Dart.


Defining a Class

To define a class in Dart, use the class keyword followed by the class name in PascalCase (e.g., BankAccount, SmartDevice).

Inside the class, you define:

  • Properties: Variables that hold the state of the object.
  • Methods: Functions that define the behavior of the object.
dart
class BankAccount { // Properties (Instance Variables) String accountNumber; String accountHolder; double balance = 0.0; // Constructor (Needed to initialize properties) BankAccount(this.accountNumber, this.accountHolder, this.balance); // Methods (Instance Behaviors) void deposit(double amount) { balance += amount; print("Deposited: \$$amount. New Balance: \$$balance"); } void withdraw(double amount) { if (balance >= amount) { balance -= amount; print("Withdrew: \$$amount. Remaining Balance: \$$balance"); } else { print("Insufficient funds for withdrawal of \$$amount!"); } } }

Creating and Using Objects

In Dart, you do not need the new keyword to create an instance of a class (although it is supported). Simply call the class constructor like a regular function.

dart
void main() { // Creating an object of BankAccount var myAccount = BankAccount("123-456-789", "Alice Smith", 500.0); // Accessing properties using dot notation print("Owner: ${myAccount.accountHolder}"); // Owner: Alice Smith print("Initial Balance: \$${myAccount.balance}"); // Initial Balance: $500.0 // Invoking methods myAccount.deposit(150.0); // Deposited: $150.0. New Balance: $650.0 myAccount.withdraw(200.0); // Withdrew: $200.0. Remaining Balance: $450.0 myAccount.withdraw(1000.0); // Insufficient funds for withdrawal of $1000! }

The this Keyword

The this keyword refers to the current instance of the class. It is primarily used to resolve name conflicts when constructor parameter names match instance variable names.

dart
class Point { double x; double y; // x and y parameters shadow the class properties x and y Point(double x, double y) { this.x = x; // Assign parameter x to instance variable x this.y = y; // Assign parameter y to instance variable y } }

Getters and Setters

Getters and setters are special methods that provide read and write access to an object's properties. They allow you to add validation logic or compute values dynamically.

dart
class Temperature { double _celsius = 0.0; // Private field (indicated by underscore) // Getter double get fahrenheit => (_celsius * 9 / 5) + 32; // Setter set celsius(double value) { if (value < -273.15) { print("Error: Temperature cannot be below absolute zero!"); } else { _celsius = value; } } } void main() { var temp = Temperature(); temp.celsius = 25.0; // Invokes setter print("Fahrenheit: ${temp.fahrenheit}"); // Invokes getter (77.0) }

Summary

  • Use class to define blueprints with properties (variables) and methods (functions).
  • Create instances without needing the new keyword.
  • Use this to refer to the current object instance inside methods and constructors.
  • Use Getters and Setters to control property access and perform data validation.
WhatsApp