Classes and Objects

Think of a class as a blueprint or template for creating objects. Just like how an architect creates a blueprint for a house, and then multiple houses can be built from that same blueprint, a class defines the structure and behavior that objects will have.

What is a Class?

A class is a template that defines:

  • Properties (also called fields or attributes) - the data that objects will store
  • Methods (also called functions) - the actions that objects can perform

What is an Object?

An object is an instance of a class - it's the actual "thing" created from the class template. You can create multiple objects from the same class, each with their own unique data.

Creating a Class in Dart

Here's how you define a basic class in Dart:

class Person {
  // Properties
  String name;
  int age;
  
  // Constructor
  Person(this.name, this.age);
  
  // Method
  void introduce() {
    print('Hi, I am $name and I am $age years old.');
  }
}

Creating Objects

Once you have a class, you can create objects (instances) from it:

void main() {
  // Creating objects
  Person person1 = Person('Alice', 25);
  Person person2 = Person('Bob', 30);
  
  // Using the objects
  person1.introduce(); // Output: Hi, I am Alice and I am 25 years old.
  person2.introduce(); // Output: Hi, I am Bob and I am 30 years old.
  
  // Accessing properties
  print(person1.name); // Output: Alice
  print(person2.age);  // Output: 30
}

Constructors

A constructor is a special method that's called when you create an object. It's used to initialize the object's properties.

class Car {
  String brand;
  String model;
  int year;
  
  // Constructor
  Car(this.brand, this.model, this.year);
  
  // Named constructor
  Car.electric(this.brand, this.model) : year = 2023;
  
  void displayInfo() {
    print('$year $brand $model');
  }
}

void main() {
  Car car1 = Car('Toyota', 'Camry', 2022);
  Car car2 = Car.electric('Tesla', 'Model 3');
  
  car1.displayInfo(); // Output: 2022 Toyota Camry
  car2.displayInfo(); // Output: 2023 Tesla Model 3
}

Class Properties and Methods

Properties store data, while methods define what the object can do:

class BankAccount {
  String accountNumber;
  double balance;
  
  BankAccount(this.accountNumber, this.balance);
  
  // Method to deposit money
  void deposit(double amount) {
    balance += amount;
    print('Deposited \$${amount}. New balance: \$${balance}');
  }
  
  // Method to withdraw money
  void withdraw(double amount) {
    if (amount <= balance) {
      balance -= amount;
      print('Withdrew \$${amount}. New balance: \$${balance}');
    } else {
      print('Insufficient funds!');
    }
  }
  
  // Method to check balance
  void checkBalance() {
    print('Current balance: \$${balance}');
  }
}

void main() {
  BankAccount account = BankAccount('12345', 1000.0);
  
  account.checkBalance(); // Current balance: $1000.0
  account.deposit(500.0); // Deposited $500.0. New balance: $1500.0
  account.withdraw(200.0); // Withdrew $200.0. New balance: $1300.0
}

Key Points to Remember

  • A class is a blueprint, an object is an instance of that class
  • Use constructors to initialize object properties
  • Properties store data, methods define behavior
  • You can create multiple objects from the same class
  • Each object has its own copy of the properties but shares the same methods

Classes and objects are fundamental concepts in object-oriented programming, making your code more organized, reusable, and easier to maintain!