Classes and Objects in Dart
Tags: flutter, dart, classesandobjects, oopsconcept
Published: 2024-01-14 17:32:34

**What are Classes and Objects?**

Imagine you are a chef running a bakery. In your bakery, a class is like a recipe card that guides you in making a specific type of treat, say, chocolate chip cookies. This recipe card (class) includes all the details on what ingredients to use, how to mix them, and how long to bake the cookies.

Now, an object is like baking actual cookies using that recipe. When you follow the instructions from the recipe card (class), you create real, delicious chocolate chip cookies (objects). You can make many batches of cookies using the same recipe card (class), each batch being a unique set of cookies.

**Why are Classes and Objects Important?**

Classes and objects help you organize your baking adventures. With different recipe cards (classes), you can bake various treats (objects), each with its own taste and style.

**Imagine:** Think of the class as your favorite recipe card for baking chocolate chip cookies, and objects are the actual batches of cookies you bake using that recipe. Each batch is a tasty creation, but they all follow the same set of instructions from your beloved recipe card.

_Let’s try to Visualize the below code:_

```
/*
 * Imagine a bakery where Chef Lily creates magical chocolate chip cookies.
 * Let's represent these cookies using a Dart class: ChocolateChipCookie.
 */

class ChocolateChipCookie {
  // Properties (ingredients) of the chocolate chip cookie
  String flavor;        // Flavor of the cookie (e.g., Classic, Double Chocolate)
  int numberOfChips;    // Number of chocolate chips in the cookie
  bool isSoft;          // Indicates whether the cookie is soft or not

  /*
   * Constructor to initialize the properties
   * This is like a special recipe card for creating a ChocolateChipCookie.
   * Chef Lily follows this recipe each time she bakes a new batch.
   */
  ChocolateChipCookie(String flavor, int numberOfChips, bool isSoft) {
    this.flavor = flavor;
    this.numberOfChips = numberOfChips;
    this.isSoft = isSoft;
  }

  /*
   * Method to describe the cookie
   * This method narrates a story about the delicious chocolate chip cookie being created.
   * Imagine customers asking, "What's special about this cookie?"
   */
  void describeCookie() {
    print('A delicious $flavor chocolate chip cookie with $numberOfChips chocolate chips.');
    if (isSoft) {
      print('This cookie is soft and chewy!');
    } else {
      print('This cookie has a nice crunch!');
    }
  }
}

/*
 * Main function where the bakery's magic unfolds
 * Imagine this as the bakery where all the delicious cookies are created.
 */
void main() {
  /*
   * Create objects (batches) of chocolate chip cookies using the class
   * Imagine Chef Lily creating different batches of cookies with her special recipes.
   */
  ChocolateChipCookie firstBatch = ChocolateChipCookie('Classic', 50, true);
  ChocolateChipCookie secondBatch = ChocolateChipCookie('Double Chocolate', 75, false);

  /*
   * Describe each batch of cookies
   * Imagine showcasing the finished batches of cookies to eager customers.
   */
  print('First Batch:');
  firstBatch.describeCookie();
  print('\nSecond Batch:');
  secondBatch.describeCookie();
}
```