In this section, we will delve into the core concepts of Object-Oriented Programming (OOP) in Dart: classes and objects. Understanding these concepts is crucial for building robust and scalable applications.

Key Concepts

  1. Class: A blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data.
  2. Object: An instance of a class. It is created using the class blueprint and can have its own state and behavior.

Creating a Class

A class in Dart is defined using the class keyword followed by the class name. Here is a simple example:

class Person {
  // Properties
  String name;
  int age;

  // Constructor
  Person(this.name, this.age);

  // Method
  void introduce() {
    print('Hi, my name is $name and I am $age years old.');
  }
}

Explanation

  • Properties: Variables that hold the state of the class. In this example, name and age are properties.
  • Constructor: A special method that is called when an object is instantiated. The constructor in this example initializes the name and age properties.
  • Method: A function defined within a class that can operate on the class's properties. The introduce method prints a message using the class properties.

Creating an Object

To create an object of the Person class, you use the new keyword (optional in Dart) followed by the class name and the required parameters for the constructor:

void main() {
  // Creating an object of the Person class
  Person person1 = Person('Alice', 30);
  
  // Calling a method on the object
  person1.introduce();
}

Explanation

  • Instantiation: Person person1 = Person('Alice', 30); creates a new object of the Person class with the name 'Alice' and age 30.
  • Method Call: person1.introduce(); calls the introduce method on the person1 object, which prints the introduction message.

Practical Exercise

Exercise 1: Define a Class and Create Objects

  1. Define a class Car with the following properties:
    • make (String)
    • model (String)
    • year (int)
  2. Add a constructor to initialize these properties.
  3. Add a method displayInfo that prints the car's details.
  4. Create two objects of the Car class and call the displayInfo method on each.

Solution

class Car {
  // Properties
  String make;
  String model;
  int year;

  // Constructor
  Car(this.make, this.model, this.year);

  // Method
  void displayInfo() {
    print('Car: $make $model, Year: $year');
  }
}

void main() {
  // Creating objects of the Car class
  Car car1 = Car('Toyota', 'Corolla', 2020);
  Car car2 = Car('Honda', 'Civic', 2018);

  // Calling methods on the objects
  car1.displayInfo();
  car2.displayInfo();
}

Explanation

  • The Car class is defined with properties make, model, and year.
  • The constructor initializes these properties.
  • The displayInfo method prints the car's details.
  • Two objects, car1 and car2, are created and their details are printed using the displayInfo method.

Common Mistakes and Tips

  • Uninitialized Properties: Ensure all properties are initialized either directly or through the constructor.
  • Method Calls: Remember to use the object name followed by a dot to call methods on an object.
  • Naming Conventions: Use meaningful names for classes, properties, and methods to make your code more readable.

Conclusion

In this section, we covered the basics of classes and objects in Dart. You learned how to define a class, create objects, and use methods to interact with the object's properties. These concepts form the foundation of Object-Oriented Programming and are essential for building complex applications.

Next, we will explore constructors in more detail, including different types of constructors and their uses.

© Copyright 2024. All rights reserved